This question already has answers here:
Add a static value to each row in a 2d array
(3 answers)
php - Add value with key to all the elements [duplicate]
(1 answer)
How can I push single element in existing array of JSON? [duplicate]
(5 answers)
Closed 11 days ago.
I have the following multidimenson array
array (
'count' => 386,
'report' =>
array (
'uuid' => '183a3956-9425-43da-845c-2839c30a951b',
'name' => 'OnlyScrumFND',
'Have' =>
array (
0 =>
array (
'uuid' => '00ad6013-4109-4940-a711-4f8fb5389e8c',
),
1 =>
array (
'uuid' => 'd651a86d-beac-498a-85a0-75ce62f28f4e',
),
),
),
)
I would like to add some info to the Array in a sublevel
foreach ($OUTPUT AS &$have['report']['Have']) {
$have['name'] = "something";
}
But it is not woking, Any hint? thanks rob
You are very close. you can try with following solution.
foreach ($OUTPUT['report']['Have'] as &$item) {
$item['name'] = "something";
}
I am using & operator here to modify the original array element in foreach loop instead of copied one.
Find the implementation below .
you can check here
Related
This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Restructure multidimensional array of column data into multidimensional array of row data [duplicate]
(3 answers)
Closed 5 years ago.
How can I convert a multidimensional array like the one below
Array(
[0] => Array(
[0]=001
[1]=002
[2]=003
)
[1] => Array(
[0]=America
[1]=Japan
[2]=South Korea
)
[2] => Array(
[0]=Washington DC
[1]=Tokyo
[2]=Seoul
)
)
into a single line array like the one below?
Array(
[0]=001,America,Washington DC
[1]=002,Japan,Tokyo
[2]=003,South Korea,Seoul
)
Here is simple code to work around,
foreach ($text as $key => $value) {
foreach ($value as $key1 => $value1) {
$result[$key1][] = $value1;
}
}
array_walk($result, function(&$item){
$item = implode(',', $item);
});
Here is the working link
array_walk — Apply a user supplied function to every member of an array
The variadiac php5.6+ version: (Offers the added benefits of not breaking on missing values and inserting null where values are missing.)
Code: (Demo)
var_export(array_map(function(){return implode(',',func_get_args());},...$text));
The non-variadic version:
Code: (Demo)
foreach($text as $i=>$v){
$result[]=implode(',',array_column($text,$i));
}
var_export($result);
Input:
$text = [
['001','002','003'],
['America','Japan','South Korea'],
['Washington DC','Tokyo','Seoul']
];
Output from either method:
array (
0 => '001,America,Washington DC',
1 => '002,Japan,Tokyo',
2 => '003,South Korea,Seoul',
)
Nearly exact duplicate page: Combining array inside multidimensional array with same key
This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 5 years ago.
I've the following array:
Array
(
[0] => Array
(
[ID] => 1
[more_user_data] => More
)
[1] => Array
(
[ID] => 2
[more_user_data] => More
)
)
Now I want to have a comma separated list of the IDs to use them in an own array. To get something like this:
array(1,2)
How could I only extract the IDs from the second array?
Use array_column() function like:
$arr = array_column($array, 'ID');
Working Example
This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 3 years ago.
Is there any predefined PHP function to find a key in a multi dimensional array?
In the below example - there is a variable name 'rose' and I need to get the key of the array by using the variable name.
The result of the key is "flower".
$array = array (
'fruits' => array (
'mango',
'cherry'
),
'flowers' => array (
'rose'
)
);
How do I achieve this?
Loop it up using a foreach
$keyword='mango';
foreach($array as $k=>$arr)
{
if(in_array($keyword,$arr))
{
echo $k;break;// "prints" fruits
}
}
Working Demo
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Custom key-sort a flat associative based on another array
(16 answers)
Closed 8 years ago.
is it possible to sort an array by keys using an custom order ?
i have an array with strings that represent the order.
$order = array('ccc','aaa','xxx','111');
$myarray = array('ccc' => 'value1','aaa' => 'value2','xxx' => 'value3',
'BBB' => 'value11','ddd' => 'value31')
now i want the array to be sorted with the elemnts with the key 'ccc' at the first position, the nthe elements with the key aaa ... and at the end should be the elements that are not in the sortlist.
is this possible ?
edit: the second 'CCC' was my fault - sorry
See this in action https://eval.in/118734
<?php
$order = array('ccc','xxx','aaa','111');
$myarray = array('ccc' => 'value1','aaa' => 'value2','xxx' => 'value3',
'ddd' => 'value31');
$temp = array();
foreach($order as $o) {
if(array_key_exists($o, $myarray)) {
$temp[$o] = $myarray[$o];
}
}
$new = array_merge($temp, $myarray);
print_r($new);
?>
I was just having a think about this as I was having a similar issue with array_multisort() and ksort().
However in your case if the snippet of code is correct, will not be possible as the second 'ccc' key with a value of 'value11' will overwrite the previous one.
php > $myarray = array('ccc' => 'value1','aaa' => 'value2','xxx' => 'value3','ccc' => 'value11','ddd' => 'value31');
php > print_r($myarray);
Array
(
[ccc] => value11
[aaa] => value2
[xxx] => value3
[ddd] => value31
)
.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to Flatten a Multidimensional Array?
THe array:
itemA => value
itemA1 => value
itemA2 => value
itemA11 => value
itemB => value
and so on...
How can I get the values gathered like this:
[0] => value (from itemA)
[1] => value (from itemA1)
[2] => value (from itemA2)
[3] => value (from itemA11)
[4] => value (from itemB)
...
?
By writing your own array function, using array_walk() ?
Or, i dont know, but maybe with array_values() ?
edit: i found this:
$a = array(1,2,array(3,4, array(5,6,7), 8), 9);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
foreach($it as $v) {
echo $v, " ";
}