This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have the following object:
stdClass Object
(
[ID] => 6
[data] => stdClass Object
(
[categories] => Array
(
[23] => Array
(
[id] => 23
[name] => A
)
[22] => Array
(
[id] => 22
[name] => B
)
[19] => Array
(
[id] => 19
[name] => C
)
)
)
I would like to print A,B,C. I managed to print 1 name:
echo $event->data->categories[19]['name']; but I would like to print all names of the array without knowing the id's.
You can use array_column and implode
echo implode(', ', array_column($event->data->categories, 'name'));
This will get all name items and implode them to a string.
You can use foreach to loop through the categories array like this:
foreach($event->data->categories as $category) {
echo $category['name'];
}
Related
This question already has answers here:
How to extract specific array keys and values to another array?
(2 answers)
Closed 2 years ago.
I have an array that looks like this:
[meta_data] => Array
[0] => Array
(
[id] => 100
[name] => John
)
[1] => Array
(
[id] => 200
[name] => Peter
)
[2] => Array
(
[id] => 300
[name] => Peter
)
What would be the simplest way to select all names from $meta_data?
Thanks.
see https://www.php.net/manual/en/function.array-column.php
var_dump(array_column($array['meta_data'], 'name'));
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
PHP - Multidimensional array to CSV
(3 answers)
How to write a multidimensional array to a csv file. PHP
(2 answers)
multidimensional irregular array to CSV in PHP
(1 answer)
Closed 3 years ago.
I need to explode stdentid array in comma separated value.currently the studentid array is in multidimensional array i need all the value should be in comma separated
The result should be for studentid array is
[studentid] => 36399,96500,96503,96509,96512 and so on..
Array
(
[started] => 1
[studentid] => Array
(
[1] => Array
(
[0] => 36399
)
[2] => Array
(
[0] => 96500
[1] => 96503
[2] => 96506
[3] => 96509
[4] => 96512
[5] => 96515
)
[3] => Array
(
[0] => 96501
[1] => 96504
[2] => 96507
[3] => 96510
[4] => 96513
)
[4] => Array
(
[0] => 96502
[1] => 96505
[2] => 96508
[3] => 96511
[4] => 96514
)
)
[name] => Test
[name_or_email] =>
[submitbtn] => 1
)
assuming your variable is $studentsResult (which contains the whole array you have posted)
in that case:
foreach($studentsResult['studentid'] as $studentId => $studentValues){
$tempStudents[$studentId] = join(',',$studentValues);
}
and then $tempStudents is what you're probably looking for\
If, however all those IDs needs to be under that ONE student, then you haven't said where is the ID, but you can follow the answer on flattening the array in one of the comments or do:
$tempStudents = [];
foreach($studentsResult['studentid'] as $someId => $studentValues){
$tempStudents = array_merge($tempStudents,$studentValues);
}
echo join(',',$tempStudents);
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I'm trying to find a value in a nested JSON using PHP. I've done a print_r() of it and get:
Array
(
[types] => Array
(
[0] => Array
(
[name] => Running
[arabicName] => الجاري
[categories] => Array
(
[0] => Array
(
[name] => Entertainment
[arabicName] => تسلية
[programs] => Array
(
[0] => Array
(
[id] => 375
[name] => Saalo Marteh
[arabicName] =>
[image] => http://plus.mtv.com.lb/Chrome/KPanel/Pictures/Programs/151120100928327.jpg
)
[1] => Array
(
[id] => 491
[name] => Celebrity Duets
[arabicName] =>
[image] => http://plus.mtv.com.lb/Chrome/KPanel/Pictures/Programs/151108084429774.jpg
)
I'm trying to get the value for name and id under the entertainment header.
I've tried the following but no luck:
echo $json['types']['categories'][0]['name'];
foreach ($json['types'][0]['categories'][0]['programs'] as $value) {
echo $value['id'] . ' ' . $value['name'];
}
This question already has answers here:
How to get an array of specific "key" in multidimensional array without looping [duplicate]
(4 answers)
Closed 8 years ago.
In PHP, is there any built-in array function that I could use to change an array...
From:
Array (
[0] => Array
(
[item_id] => 1
)
[1] => Array
(
[item_id] => 3
)
[2] => Array
(
[item_id] => 2
)
)
To:
Array
(
[item_id] => Array ( [0] => 1, [1] => 2, [2] => 3 )
)
Thanks for your help.
Even if you cant find a built-in function, you can write a small loop. Although im sure this can also be done with array_map
foreach($yourArray as $subArray)
{
$newArray["item_id"][]=$subArray["item_id"];
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I merge PHP arrays?
I have two arrays, both results of db queries. I have a simple example below (no the real data- just for demo purposes. The real data is significantly more complex).
$results:
Array
( [0] =>
Array ( [id] => 20 [age] => 29 )
[1] =>
Array ( [id] => 593 [age] => 38 )
)
$persons:
Array
( [0] =>
Array ( [id] => 593 [name] => Jack Jones )
[1] =>
Array ( [id] => 20 [name] => John Smith )
)
My question is: how can I match the $persons[name] to replace $results[id] so that I end up with:
$results:
Array
( [0] =>
Array ( [id] => John Smith [age] => 29 )
[1] =>
Array ( [id] => Jack Jones [age] => 38 )
)
the arrays are unorderd - I need to replace values if the keys match (and yes, each key in $results definitely has a corresponding entry in $persons). Any help much appreciated!
$a = array(
array('id'=>58,'name'=>'name1'),
array('id'=>63,'name'=>'name2'),
);
$b = array(
array('id'=>63,'value'=>'value2'),
array('id'=>58,'value'=>'value1'),
);
//making key-value
foreach(array_values($a) as $tmp)
{
$aProcessed[$tmp['id']]=$tmp['name'];
}
foreach(array_values($b) as $tmp)
{
$bProcessed[$tmp['id']]=$tmp['value'];
}
//uncomment to see key-value arrays
//var_dump($aProcessed,$bProcessed);
//combining
foreach($aProcessed as $key=>$value)
{
$result[]=array('name'=>$aProcessed[$key],'value'=>$bProcessed[$key]);
}
var_dump($result);