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);
Related
I have two arrays and would like to combine / merge / put them together.
$arr1 = array(
0 => array(1, 2),
1 => array(5, 6)
);
$arr2 = array(
0 => array(2, 3),
1 => array(6, 7)
);
come_together_right_now($arr1, $arr2); // the missing function?
and the result would be:
Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array (
[0] => 5
[1] => 6
[2] => 7
)
There are way too many array functions! array_merge and array_combine and the recursive alternatives seem to replace the values and they don't preserve numeric keys. How do I do this?
Assuming that they will always have the same keys!
$result = array();
foreach($arr1 as $key=>$array) {
$result[$key] = array_merge($array, $arr2[$key]);
}
I might be late of answering this question but this might help you simply using array_map,array_merge and array_unique function like as
$result = array_map('array_unique',array_map('array_merge',$arr1,$arr2));
print_r($result);
Output
Array (
[0] => Array (
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array (
[0] => 5
[1] => 6
[2] => 7
)
Demo
Synchronously iterate the arrays to access their rows.
Merge rows, remove duplicates, then re-index the elements.
Code: (Demo)
var_export(
array_map(
fn(...$rows) => array_values(array_unique(array_merge(...$rows))),
$arr1,
$arr2
)
);
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);
Hi I have below multidimensional arrays -
Array
(
[user_attempts] => 0
[2] => Array
(
[0] => 1
[1] => 4
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
)
[1] => Array
(
[0] => Earth
)
)
and my second array is like below
Array
(
[1] => Array
(
[0] => Earth
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
[1] => asdas
)
)
I need to intersect both multidimensional arrays - so the result would be
Array
(
[1] => Array
(
[0] => Earth
)
[3] => Array
(
[0] => 32
[1] => 23
)
)
Can anyone help me to sort this out.
What I have tried is using array_intersect() but it limits to single array not multidimensional i guess.
PHP comes with a ton of functions already built in, but sometimes you still have to implement things yourself. What you want to do can be easily done by using the existing functions.
The goal is to do the following steps:
Find the keys that exist in both arrays
Loop through the array using these keys
Take the items of both input arrays with each of these keys
Calculate the intersection of those two arrays
Put it into a result array
Here is one way to do this:
function array_intersect_2dim (array $a1, array $a2) {
$keys = array_intersect(array_keys($a1), array_keys($a2));
$return = array();
foreach ($keys as $key) {
$return[$key] = array_intersect($a1[$key], $a2[$key]);
if (sizeof($return[$key]) == 0) {
unset($return[$key]);
}
}
return $return;
}
It works only for two dimensions. If you need more, you have to build a recursive approach, which follows the exact same principle.
To make the easier to compare you can use serialize/unserialize on this one. And then use array_intersect(). Try this example: Sample Output
$array1 = array( 'user_attemps' => 0, 2 => array(1, 4), 3 => array(32, 23), 4 => array('asdsa'), 1 => array('Earth'),);
$array2 = array( 1 => array('Earth'), 2 => array(2, 3), 3 => array(32, 23), 4 => array('asdsa', 'asdas'),);
$result = array_map('unserialize',array_intersect(array_map('serialize', $array1), array_map('serialize', $array2)));
print_r($result);
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;
}
I have this multi-dimensional PHP array (below).
Array
(
[0] => Array
(
[2] => one#example.com
)
[1] => Array
(
[4] => two#example.com
)
[2] => Array
(
[3908] => twenty#example.com
)
[3] => Array
(
[2548] => eleven#example.com
)
[4] => Array
(
[3099] => ten#example.com
)
[5] => Array
(
[5283] => six#example.com
)
)
I was wondering how could I merge? or combine? or simply do it like this using PHP (below).
Array
(
[2] => one#example.com
[4] => two#example.com
[3908] => twenty#example.com
[2548] => eleven#example.com
[3099] => ten#example.com
[5283] => six#example.com
)
Help
You can "promote" the second level elements to first level elements via call_user_func_array(). And while array_merge() re-indexes the array, array_replace() does not but retains the original keys which makes this a one-liner:
$a = call_user_func_array('array_replace', $a);
test:
<?php
$a = array (
array (2 => 'one#example.com'),
array (4 => 'two#example.com'),
array (3908 => 'twenty#example.com'),
array (2548 => 'eleven#example.com'),
array (3099 => 'ten#example.com'),
array (5283 => 'six#example.com'),
);
$a = call_user_func_array('array_replace', $a);
var_dump($a);
Simply go through the array and store to another.
$a = array( 0 => array(1) , 1=> array(3) );
$another=array();
foreach ($a as $k=>$v){
foreach ($v as $y=>$z){
$another[$k]=$z;
}
}
print_r($another);
How about this?
foreach( $old_arr as $old )
{
$key = key( $old[0] );
$new[ $key ] = $old[0][$key];
}
EDIT: Never mind didn't realize previous answer was corrected.