php - How to get type from this array -


i have following array want type , (if set) amount value (default amount should 1). how do so?

array(10)  {   [0]=> array(1) { ["type"]=> string(3) "empty" }  [1]=> array(1) { ["type"]=> string(7) "beer" }  [2]=> array(2) { ["type"]=> string(8) "beef" ["amount"]=> int(3) }  [3]=> array(1) { ["type"]=> string(3) "wood" }  [4]=> array(1) { ["type"]=> string(3) "cheese" }  [5]=> array(1) { ["type"]=> string(3) "empty" }  [6]=> array(1) { ["type"]=> string(3) "empty" }  [7]=> array(1) { ["type"]=> string(3) "empty" }  [8]=> array(1) { ["type"]=> string(3) "empty" }  [9]=> array(1) { ["type"]=> string(3) "empty" }   }  

edit: well, i've tried foreach, did before.. since no json anymore died

foreach($json $key => $morejson) {     $json[$key] = json_decode($morejson); }  echo '<p> user data player 1 ', $json['type'][3]; 

but gave me:

warning: json_decode() expects parameter 1 string, array given in xx on line 19 

you this

$new_array = array(); foreach($array $item){     if( !array_key_exists('amount',$item) ){         $item['amount'] = 1;     }     $new_array[] = $item; } 

or better without curly braces, doing 1 line of code in if statement.

$new_array = array(); foreach($array $item){     if( !array_key_exists('amount',$item) )         $item['amount'] = 1;     $new_array[] = $item; } 

or better without $new_array , braces. can use & assign array value reference.

foreach($array &$item)     if( !array_key_exists('amount',$item) )         $item['amount'] = 1; 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -