associative array to array php code example

Example 1: php transform associative array to array

$array = array_values($array);

Example 2: convert object to array php

<?php 
class sample { 
      
    /* Member variables */
    var $var1; 
    var $var2; 
      
    function __construct( $par1, $par2 )  
    { 
        $this->var1 = $par1; 
        $this->var2 = $par2; 
    } 
} 
  
// Creating the object 
$myObj = new sample(1000, "second"); 
echo "Before conversion: \n"; 
var_dump($myObj); 
  
// Converting object to associative array 
$myArray = json_decode(json_encode($myObj), true); 
echo "After conversion: \n"; 
var_dump($myArray); 
?> 
  
Output:
Before conversion: 
object(sample)#1 (2) {
  ["var1"]=>
  int(1000)
  ["var2"]=>
  string(6) "second"
}

After conversion: 
array(2) {
  ["var1"]=>
  int(1000)
  ["var2"]=>
  string(6) "second"
}

Example 3: how to store array in variable php

$myArray = array("http://www.marleenvanlook.be/admin.php","http://www.marleenvanlook.be/checklogin.php","etc");

$i = 0;
foreach($myArray as $value){
    ${'something'.$i} = $value;
    $i++;
    }

echo $something0; //http://www.marleenvanlook.be/admin.php

Example 4: php take out 2 level array key value

foreach($bigArray as $array){    
    foreach($array as $key=>$value){
        echo $key;
    }
}


#If if helps you give it Thumbs up

Tags:

Php Example