This question already has answers here:
Generate an associative array from an array of rows using one column as keys and another column as values
(3 answers)
Closed 17 days ago.
I am trying to look for the most 'elegant' way to reduce a multi-dimensional array
FROM:
Array
(
[0] => Array
(
[id] => 207
[location] => 22
[target] => 90
)
[1] => Array
(
[id] => 208
[location] => 71
[target] => 89
)
)
INTO:
Array
(
[22] => 90
[71] => 89
)
without having to do something like this:
foreach ($x as $b) {
$a[$b['location']] = $b['target'];
}
Use array_column() to get the contents of the location and target columns, and then combine them into key/value with array_combine().
$a = array_combine(array_column($x, 'location'), array_column($x, 'target'));
Related
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'];
}
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 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:
Sorting multidim array: prioritize if column contains substring, then order by a second column
(4 answers)
Sort multidimensional array by multiple columns
(8 answers)
Closed 9 years ago.
I have this Array
Array (
[0] => Array
(
[id] => 61
[testo] => articolo di maggio
[data] => 2013-05-03
[orario] => 00:00:00
[nome_files] => fabmad_1920x1200.jpg
[pubblicato] => 1
)
[1] => Array
(
[id] => 58
[testo] =>
[data] => 2013-06-03
[orario] => 00:00:00
[nome_files] => 20130603100647_da_installare.rtf
[pubblicato] => 1
)
[2] => Array
(
[id] => 59
[testo] => Demo
[data] => 2013-06-03
[orario] => 00:00:00
[nome_files] => eye_drops_water_2.jpg
[pubblicato] => 1
)
)
I want to sort it by "data".
I want to display the "data" and for each data the elements...
Try the ksort function or the array-multisort
Check the php documentation: http://php.net/manual/en/function.ksort.php
http://php.net/manual/en/function.array-multisort.php
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);