Get all values from a multidimensional array into a single array [duplicate] - php

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, " ";
}

Related

Add Key and Value to a multidimesnsion array PHP [duplicate]

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

How to calculate difference between keys and values in php [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have two types of arrays:
1:
$array1["a"][] = "value1";
$array1["a"][] = "value2";
$array1["b"][] = "value3";
2:
$array2["0"] = "a";
What I need now is to somehow find difference between these two arrays. I need to filter out array1 by key, which is located in array2 value. I have tried doing the following:
array_diff(array_keys($array1), array_values($array2));
But I get the following error on that line:
ErrorException Array to string conversion
Any ideas?
Something like this?
foreach ($array1 as $key => $value)
if( array_search ($key , $array2 ))
unset($array1[$key]);
If $array1 needs to have the values, you just need to put the diff in $array1 :
$array1 = array_diff(array_keys($array1), array_values($array2));
Depending on how you constructed your arrays, it should work. The following code (based on your question) worked:
<?php
$array1=array("a" => array(),"a" => array(),"b" => array());
$array2=array("0"=>"a");
print_r(array_keys($array1));
echo("<br/>");
print_r(array_values($array2));
echo("<br/>");
print_r(array_diff(array_keys($array1), array_values($array2)));
>
This results in:
Array ( [0] => a [1] => b )
Array ( [0] => a )
Array ( [1] => b )

How to convert a multidimensional array into a single line array in PHP? [duplicate]

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

PHP multidimensional associative array [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 3 months ago.
I am new to php & I'm not sure that this can be done, but I am hoping that someone knows how to. I've collected all the data that I need to submit but now I need to reformat it before I can json_encode to send to the database.
Basically, I have 1 parent array($data) containing 3 sub-arrays ($hours, $WId, $Pid). I need to create associative arrays for each index position & join them together.
Here is my parent array:
$data = array(
'hours' => array(),
'wId' => array(),
'phaseId' => array(),
);
Here is what currently returns when I print_r each of these arrays:
Array ( [hours] => Array ( [0] => 0.5 [1] => 1 [2] => 2 ) )
Array ( [wId] => Array ( [0] => 10, [1] => 9, [2] => 8, ) )
Array ( [phaseId] => Array ( [0] => 20, [1] => 20, [2] => 19, ) )
I need to take these "vertical" arrays & turn them in to "horizontal" arrays per index, using thearray name as the $key & the value for that index as $value. Here is what I need to return.... (Syntax is probably wrong but you can get the idea.)
Array[1] ("hours" => 0.5, "wId" => 10, "phaseId" => 20)
Array[2] ("hours" => 1, "wId" => 9, "phaseId" => 20)
Array[3] ("hours" => 2, "wId" => 8, "phaseId" => 19)
Is there a function that will allow me to do this easily? I saw how to join & merge them together but not sure how to set the array name (hours, etc) as the $key & the value for each index as $value. I need to loop it too because the length of the arrays will vary. (But they will always the same length as each other, so index should still work as what needs to be collected.)
Any suggestions would be greatly appreciated :)
<?php
// set up your output array
$result = array();
// loop through $data, exposing $name for later use
foreach ($data as $name => $array) {
// loop through each named array and set the desired value
// using the current $key and $name
foreach ($array as $key => $value) {
$result[$key][$name] = $value;
}
}
// tada!
print_r($result);
NOTE: In your desired results in your question, you had the parent Array keys starting at 1. This answer assumes that's a typo and you actually wanted them to match the input. If you indeed wanted it to start at one, just change this line in my answer:
$result[$key+1][$name] = $value;

sort array by keys using an array with sortstring [duplicate]

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
)
.

Categories