My sorted array needs to be split up so that each created array has all of those associated with that value.
Array (
[NAME] => Array
(
[0] => Fooaz
[1] => bzdsfdasfz
[2] => Fooooooooo
)
[DESCRIPTION] => Array
(
[0] => Foo
[1] => Foo
[2] => Barrrr
)
)
For example, from that array I want to get two arrays. One containing:
[NAME]=>Array([0] => Fooooooooo), [DESCRIPTION]=>Array([0] => Barrrr):
The other containing the remaining elements.
What's an efficient way of doing this?
$arr = Array (
'NAME' => Array
(
0 => 'Fooaz',
1 => 'bzdsfdasfz',
2 => 'Fooooooooo'
),
'DESCRIPTION' => Array
(
0 => 'Foo',
1 => 'Foo',
2 => 'Barrrr'
)
);
$arr1 = array();
foreach ($arr as $key => $value) {
$arr1[$key][] = array_pop($arr[$key]);
}
print_r($arr);
print_r($arr1);
Use array_pop():
http://codepad.org/GeP6OEtf
<?php
$arr=array(
"NAME"=>array('a','b','c','d'),
"DESC"=>array('A','B','C','D')
);
$newarr=array();
foreach ($arr as $key=>$value) {
$newarr[$key]=array(array_pop($arr[$key]));
}
print_r($arr);
echo "\n-------------\n";
print_r($newarr);
You can use extract function.
extract($array);
this will give you two arrays automatically.
reference
Related
I've been stuck on this for the better part of the day and I'm out of ideas. I have an array like this:
Array
(
[rank] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[name] => Array
(
[0] => 'Hideki'
[1] => 'Rory'
[2] => 'Sam'
[money] => Array
(
[0] => '$100'
[1] => '$200'
[2] => '$500'
)
)
and I have the task to create an array with the following format from it:
Array
(
[Hideki] => Array
(
[rank] => 1
[money] => '$100'
)
[Rory] => Array
(
[rank] => 2
[money] => '$200'
[Sam] => Array
(
[rank] => 3
[money] => '$500'
)
)
The catch is that 'rank' and 'money' have to be dynamic names
It should be simple as that:
$new = [];
foreach($array['name'] as $key => $name) {
$new[$name] = [
'rank' => $array['rank'][$key],
'money' => $array['money'][$key]
];
}
Little late but here my answear. My approach was to use the array_walk() function.
$array = [
'rank' => [1,2,3],
'name' => ['Hideki', 'Rory', 'Sam'],
'money' => ['$100', '$200', '$500'],
];
$i = 0;
$newArray = [];
array_walk($array['name'], function($name) use (&$i, $array, &$newArray) {
$newArray[$name] = ['rank'=> $array['rank'][$i], 'money' => $array['money'][$i]];
$i++;
});
print_r($newArray);
Run your first array through a foreach loop referencing only the "name" key and using key=>value pairs. Then reference the other keys from the first array when you build the new array, setting the value as the key to the second array.
You will need to first get the keys using array_keys() and use a nested foreach to loop through all the keys.
Example:
$keys1 = array_keys($array1);
foreach ($array1['name'] as $key => $value) {
$val2 = array();
foreach ($keys1 as $k){
if ($k != 'name') $val2[$k] = $array1[$k][$key];
}
$array2[$value] = $val2;
}
Below is the print_r($result) result of an $result = array_merge(array($id => $value), array($user => $value), array("Information" => $value)) variable in a piece of PHP code:
Array ( [id] => 1 [user] => 1
[Information] => Array ( [0] => Array ( [name] => 'John' )
[1] => Array ( [family] => 'Goldenberg' )
[2] => Array ( [age] => '21' )))
How to remove the array keys from the "Information" => $value array in PHP to make the output like below:
Array ( [id] => 1 [user] => 1
[Information] => Array ([name] => 'John'
[family] => 'Goldenberg'
[age] => '21'))
Is there any specific function in PHP to complete this task? Thanks a lot for your help.
Your "Information" array is a multidimensional one, having some arrays as the elements, in which there are some "key-value" pairs as "data". You may use the following to reinsert the "data" in the desirable way:
<?php
$info = array( array('name'=>'John'), array('family' => 'Goldenberg'), array('age' => 21));
$out = array();
foreach($info as $arr)
foreach($arr as $key => $val)
$out[$key] = $val;
print_r($out);
?>
I have two arrays :
Array 1
(
[0] => 976504282322
[1] => 976530000022
)
=====
Array 2
(
[0] => Array
(
[Mobile] => 978504282398
[Name] => Mike
)
[1] => Array
(
[Mobile] => 976504282300
[Name] => Jhon
)
)
====
I want to merge them and the final result should be :
===
Array3
(
[0] => Array
(
[Mobile] => 978504282398
[Name] => Mike
)
[1] => Array
(
[Mobile] => 976504282300
[Name] => Jhon
)
[2] => Array
(
[Mobile] => 976504282322
)
[3] => Array
(
[Mobile] => 976504282322
)
)
Note : performance is very important here because each array have over 200,000 item , so using for loop is not a good idea.
my code :
foreach ($Array1 as $mobile=> $value) {
$Array2[]['Mobile']=$value;
}
Here is a oneliner:
$a = array(1,2,3);
$b = array(array('Mobile' => 1, 'name' => 'Mike'));
var_dump(
array_merge($b, array_map(function($i) { return array('Mobile' => $i); }, $a))
);
PS: even though you don't see a loop explicitly - php performs it internally.
PPS: personally I would create a performance test to compare a straightforward for base solution. And I'm sure it will be better (and will consume less memory without doubts)
Why not Array_Merge ? Especially for such tasks, merging multiple arrays.
But you will need to store
Array 1
(
[0] => 976504282322
[1] => 976530000022
)
as
Array 1
(
[0] => array('Mobile'=>976504282322)
[1] => array('Mobile'=>976530000022)
)
and then you are ready to merge.
anywawys should be faster than looping 200k array.
In case you don't care for array indexing, you could also try
$arr = $arr + $arr2; // appending
Try this function:
function my_array_merge() {
$new = array();
foreach (func_get_args() as $arr) {
foreach ($arr as $item) {
if (!is_array($item)) {
$item = array('Mobile' => $item);
}
$new[] = $item;
}
}
return $new;
}
Example:
$arr1 = array(
'976504282322',
'976530000022',
);
$arr2 = array(
array(
'Mobile' => '978504282398',
'Name' => 'Mike',
),
array(
'Mobile' => '976504282300',
'Name' => 'John',
),
);
print_r(my_array_merge($arr2,$arr1));
this worked good
<?php
$array = array("a" , "b", "c" ,"d");
$array1 = array(1 , 2, 3 ,4);
$sum=array();
for($i=0;$i<count($array);$i++)
{
$sum[$i][1]=$array[$i];
$sum[$i][2]=$array1[$i];
}
echo '<pre>';
print_r($sum);
?>
I'm looking for an elegant way to turn this array:
Array (
[foo] => 1
[bar] => 1
[zim] => 3
[dib] => 6
[gir] => 1
[gaz] => 3
)
Into this array:
Array (
[1] => Array ( foo, bar, gir ),
[3] => Array ( zim, gaz ),
[6] => Array ( dib )
)
Note:, there is no relationship between the keys or values. They are completely arbitrary and used as examples only. The resulting array should be an associative array grouped by the values of the input array.
Thanks!
$input = array(
'foo' => 1,
'bar' => 1,
'zim' => 3,
'dib' => 6,
'gir' => 1,
'gaz' => 3
)
$output = array();
foreach ( $input as $k => $v ) {
if ( !isset($output[$v]) ) {
$output[$v] = array();
}
$output[$v][] = $k;
}
I think this will do it just fine:
foreach ($arr1 as $k => $val) $arr2[$val][] = $k;
where $arr1 is the original array outputting the new array to $arr2.
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.