How to Get Single Value from This Multi-dimensional PHP Array

How to echo single value from multidimensional array when key is numeric?

It should be

echo $array['events'][0]['event']['category'];

Multidimensional array - how to get specific values from sub-array

You can populate the first select this way:

<select>

<?php $c=count($array);
for ( $i=0; $i < $c; $i++)
{ ?>
<option><?php echo $array[$i]['name'];?></option>
<?php } ?>

</select>

2nd select:

<select>

<?php
for ( $i=0; $i < $c; $i++)
{
$c2=count($array[$i]);
for ($j=0;$j<$c2;$j++){
?>
<option><?php echo $array[$i][$j]['name'];?></option>
<?php }} ?>

</select>

how to get all values from an multi dimensional array

Something like this should do it:

$result = [];
array_walk_recursive($input, function($value, $key) use(&$result) {
if ($key === 'id') {
$result[] = $value;
}
});

How to get unique value in multidimensional array

Seems pretty simple: extract all pid values into their own array, run it through array_unique:

$uniquePids = array_unique(array_map(function ($i) { return $i['pid']; }, $holder));

The same thing in longhand:

$pids = array();
foreach ($holder as $h) {
$pids[] = $h['pid'];
}
$uniquePids = array_unique($pids);

Get all values from multidimensional array

Try this:

function get_values($array){
foreach($array as $key => $value){
if(is_array($array[$key])){
print_r (array_values($array[$key]));
}
}
}

get_values($array);

How to get an array of specific key in multidimensional array without looping

Since PHP 5.5, you can use array_column:

$ids = array_column($users, 'id');

This is the preferred option on any modern project. However, if you must support PHP<5.5, the following alternatives exist:

Since PHP 5.3, you can use array_map with an anonymous function, like this:

$ids = array_map(function ($ar) {return $ar['id'];}, $users);

Before (Technically PHP 4.0.6+), you must create an anonymous function with create_function instead:

$ids = array_map(create_function('$ar', 'return $ar["id"];'), $users);

php print a single value from a multidimensional array

Simple it that

$array = json_decode($json,true);
print_r($array);

echo $array['data']["language"] ;

results

Array
(
[data] => Array
(
[country] => USA
[currency] => USD
[language] => American_English
)

)

American_English

How to get single value from this multi-dimensional PHP array

Look at the keys and indentation in your print_r:

echo $myarray[0]['email'];

echo $myarray[0]['gender'];

...etc

php multidimensional array get values

This is the way to iterate on this array:

foreach($hotels as $row) {
foreach($row['rooms'] as $k) {
echo $k['boards']['board_id'];
echo $k['boards']['price'];
}
}

You want to iterate on the hotels and the rooms (the ones with numeric indexes), because those seem to be the "collections" in this case. The other arrays only hold and group properties.



Related Topics



Leave a reply



Submit