Given that I have these arrays:
$array1:
Array
(
[0] => Title1
[1] => Title2
[2] => Title3
[3] => Title4
...
$array2:
Array
(
[0] => A
[1] => B
[2] => C
[3] => D
...
$array3:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
...
I want to convert all the upper arrays into one Multidimensional Array that looks like this:
Array
(
[0] => Array
(
[0] => Title1
[1] => A
[2] => 1
)
[1] => Array
(
[0] => Title2
[1] => B
[2] => 2
)
[2] => Array
(
[0] => Title3
[1] => C
[2] => 3
)
...
I have this code that does what I want but is excessive and inefficient:
$result1 = array();
foreach($array1 as $key => $value) {
$tmp = array($value);
if (isset($array2[$key])) {
$tmp[] = $array2[$key];
}
$result1[] = $tmp;
}
$result2 = array();
$i=0;
foreach($result1 as $value){
$result2[$i] = $value;
$result2[$i][] = $array3[$i];
$i++;
}
print_r($result2);
In terms of efficiency, how can I improve my code? Can this be done all in one "foreach"? What about if I have ten or even more simple arrays? If this is the case, using my code I would have to copy down the second foreach and change the variables for each other array that comes after the first two arrays.
This should work for you:
Just use array_map() to loop through all arrays at once, e.g.
$result = array_map(function($v1, $v2, $v3){
return [$v1, $v2, $v3];
}, $array1, $array2, $array3);
Or you can use call_user_func_array(), so if you expand you only have to add the variables to the array and don't have to add the arguments in the anonymous function:
$result = call_user_func_array("array_map", [NULL, $array1, $array2, $array3]);
array_map() is the way to go but it's much easier:
$result = array_map(null, $array1, $array2, $array3);
Related
I have an array which has multiple arrays inside of like. Here is how it looks like:
Array (
[0] => Array (
[0] => s1
[1] => s2
[2] => s5
[3] => s1
[4] => s25
[5] => s1
[6] => s6
[7] => s6
[8] => s1
)
[2] => Array (
[0] => a2
[1] => a1
[2] => a4
)
[3] => Array ( )
[4] => Array ( )
)
What I'm trying to figure out is how I can turn these multiple arrays into 1 string where is has values from all arrays split with commas $values = "s1,s2,s5.."
I used impode() before but with this type of array, it's not functioning. Another problem in this is empty arrays which i believe can be removed with array_filter().
$destination_array = array_filter($tags_list);
$destination_array = implode(",", $tags_list);
print_r($destination_array);
You have a two dimensional array here. And neither implode() or array_filter() work with multidimensional arrays.
This means you filter all empty values out of the first dimension and also try to implode the first dimension:
Array (
[0] => Array (
[0] => s1
[1] => s2
[2] => s5
[3] => s1
[4] => s25
[5] => s1
[6] => s6
[7] => s6
[8] => s1
)
[2] => Array (
[0] => a2
[1] => a1
[2] => a4
)
[3] => Array ( )
[4] => Array ( )
↑ Filter first dimension and implode it
)
So obviously what you have to do is, you have to filter each subArray. Then implode each subArray and implode all strings together again.
How can we do this? Just use array_map().
With array_map() you go through each subArray and first filter all empty values out with array_filter(). Then you implode() each subArray to a string. After this you will end up with an array like this:
Array
(
[0] => s1,s2,s5,s1,s25,s1,s6,s6,s1
[2] => a2,a1,a4
)
And then you have to implode it again to get 1 string out of it.
Code:
echo implode(",", array_filter(array_map(function($v){
return implode(",", array_filter($v));
}, $array)));
output:
s1,s2,s5,s1,s25,s1,s6,s6,s1,a2,a1,a4
The desired result can be achieved a number of ways (beyond Rizier's function approach which is good and clean):
Demo
Method #1: the boring / loopy / fast way
$array = [
['s1','s2','s5','s1','s25','s1','s6','s6','s1'],
2 => ['a2','a1','a4'],
[],
[]
];
$result = [];
foreach ($array as $subarray) {
if ($subarray) {
$result[] = implode(',', $subarray);
}
}
echo implode(',', $result);
Method #2: the handy "leafnode" grabber
array_walk_recursive(
$array,
function($v) {
static $first;
echo $first . $v;
$first = ',';
}
);
Method #3: the slower, unorthodox regex ways:
echo implode(
',',
preg_match_all(
'/[a-z]\d+/',
json_encode($array),
$out
)
? $out[0]
: []
);
And
echo implode(
',',
preg_split(
'/\W+/',
json_encode(array_values($array)),
0,
PREG_SPLIT_NO_EMPTY
)
);
Output (from each of the above:
s1,s2,s5,s1,s25,s1,s6,s6,s1,a2,a1,a4
I think this will work:
$str='';
foreach($array as $k=>$v){
if(is_array($v)){
$str.=implode(',',$v);
}
}
how to transform two arrays. could this be possible?
to make arrays individually in php. how to transform two arrays. could this be possible?
to make arrays individually in php.
Arrayone
(
[0] => H00
[1] => T00.0
[2] => L00
)
Arraytwo
(
[0] => 1
[1] => 2
[2] => 3
)
Transform to like this
Array
(
[icd] => H00
[rank] => 1
)
Array
(
[icd] => T00.0
[rank] => 2
)
Array
(
[icd] => L00
[rank] => 3
)
Assume you want to have an array containing all the transformed arrays inside.
$array1 = array('H00','T00.0','L00');
$array2 = array('1','2','3');
$result = array();
$array3 = array_combine($array2, $array1);
foreach($array3 as $key => $value)
$result[] = array('icd' => $value, 'rank' => $key);
print_r($result);
Try this..
You can able to combine two arrays in PHP.
$arr3 = array_combine($arr2, $arr1);
print_r($arr3);
I've an array in php something like below
Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
)
Now I want to remove the parents indexes 0,1... and finally want to get all the values (only unique values).
Thanks.
One possible approach is using call_user_func_array('array_merge', $arr) idiom to flatten an array, then extracting unique values with array_unique():
$new_arr = array_unique(
call_user_func_array('array_merge', $old_arr));
Demo. Obviously, it'll work with array of any length.
$startArray = Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
);
//Edited to handle more the 2 subarrays
$finalArray = array();
foreach($startArray as $tmpArray){
$finalArray = array_merge($finalArray, $tmpArray);
}
$finalArray = array_unique($finalArray);
Using RecursiveArrayIterator Class
$objarr = new RecursiveIteratorIterator(new RecursiveArrayIterator($yourarray));
foreach($objarr as $v) {
$new_arr[]=$v;
}
print_r(array_unique($new_arr));
Demo
OUTPUT:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
$new_array = array_merge($array1, $array2);
$show_unique = array_unique($new_array);
print_r($show_unique);
array_merge is merging the array's, array_unique is removinge any duplicate values.
Try something like this:
$new_array = array();
foreach($big_array as $sub_array) {
array_merge($new_array, $sub_array);
}
$new_array = array_unique($new_array);
(code not tested, this just a concept)
Try this:
$Arr = array(array(40173, 514081, 363885, 891382),
array(40173,5181, 385,891382));
$newArr = array();
foreach($Arr as $val1)
{
foreach($val1 as $val2)
{
array_push($newArr, $val2);
}
}
echo '<pre>';
print_r(array_unique($newArr));
Output:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
Refer: https://eval.in/124240
--
Thanks
I have 2 arrays, that I want to put into 1 multidimensional array
$array_result = array();
Array1 = a,b,c,d
Array2 = 1,2,3,4
The result that I want to get is
$array_result = [0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
) etc...
I can't work out how to do this. Then length of Array1 and Array2 varies as it is dynamic data.
Can someone point me in the right direction?
Try this
$arr1 = array(1,2,3,4);
$arr2 = array('a','b','c','d');
$arr3 = array();
for($i = 0;$i< count($arr1);$i++) {
$arr = array();
$arr[] = $arr2[$i];
$arr[] = $arr1[$i];
array_push($arr3,$arr);
}
Output
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 3
)
[3] => Array
(
[0] => d
[1] => 4
)
)
Codepad Demo
Use array_merge() function. It should do what you want to do.
$array_result=array_merge($array1, $array2, ...);
I have two same-length arrays like this:
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
And I want to end up with this:
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 3
)
)
array_combine would make one set of the above values into array keys, which I don't want -- I want both to end up as array values, combining each item of the two arrays into a new array.
Is there a built in function to do this or do I have to roll my own?
Try this:
$result = array();
foreach ($array1 as $i => $val) {
$result[] = array($val, $array2[$i]);
}
http://codepad.viper-7.com/Jx5H1Q
Is there a built in function to do this
Yes
or do I have to roll my own?
No
By calling array_map() and feeding it null as the callback parameter, then feeding it 2 or more arrays, it will restructure your data as desired.
Code: (Demo)
$array1 = ['a', 'b', 'c'];
$array2 = [1, 2, 3];
var_export(array_map(null, $array1, $array2));
Output:
array (
0 =>
array (
0 => 'a',
1 => 1,
),
1 =>
array (
0 => 'b',
1 => 2,
),
2 =>
array (
0 => 'c',
1 => 3,
),
)
If you had string keys, you could use array_merge_recursive to merge them. As it is, though, you'll need to do something else. For instance:
$result = Array();
$arrays = Array($array1,$array2...);
foreach($arrays as $arr) {
foreach($arr as $k=>$v) $result[$k][] = $v;
}