Does anyone have an idea how to merge and sort array in PHP?
I have array "AAA". When I print:
Array
(
[AAA] => Array
(
[0] => Array
(
[0] => a
[1] => c
)
[1] => Array
(
[0] => b
)
)
)
How can I merge this array to get result like:
Array
(
[AAA] => Array
(
[0] => a
[1] => b
[2] => c
)
)
Thanks.
Try this code, i have tried this code this is working as you expect.
Example:-
<?php
$array = array('AAA' => array(array('0' => "a",'1' => "b"), array('0' => "b")));
//Before Sorting
echo "Before sorting****".'<pre>';
print_r($array);
echo '</pre>';
$my_array = array_merge($array['AAA'][0], $array['AAA'][1]);
//After Sorting
echo "After sorting****".'<pre>';
print_r($my_array);
echo '</pre>';
?>
Check Out the Demo
Output:-
Before sorting****
Array
(
[AAA] => Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => b
)
)
)
After sorting****
Array
(
[0] => a
[1] => b
[2] => b
)
You could first merge using array_merge and then sort using sort
$temp = array_merge($arr['AAA'][0], $arr['AAA'][1]);
sort($temp);
Use this.
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
$l = iterator_to_array($it, false);
var_dump($l);
Related
I have array 1 like this
Array
(
[0] => 1
[1] => 2
)
Second array would be
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
)
[1] => Array
(
[FullName] => Dvs Patel
)
)
I want to merge it the way values would be added to second array with same keys. Desired Output will look like this or some way around so that I can use the Array 1's value with Second Array Only:
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
[0] => 1
)
[1] => Array
(
[FullName] => Dvs Patel
[1] => 2
)
)
You can apply simple foreach() to do that
$final = [];
foreach($array2 as $key =>$arr2 ){
$final[$key]['FullName'] = $arr2['FullName'];
$final[$key][$key] = $array1[$key];
}
print_r($final);
Output:- https://eval.in/1010437
If both arrays are of the same length, you might use array_map passing the array_keys as the second parameter:
$array1 = ["1", "2"];
$array2 = [
["FullName" => "Bhupat Chippa"],
["FullName" => "Dvs Patel"]
];
$result = array_map(function($x, $y) use ($array1){
$x[$y] = $array1[$y];
return $x;
}, $array2, array_keys($array1));
print_r($result);
Demo
That will give you:
Array
(
[0] => Array
(
[FullName] => Bhupat Chippa
[0] => 1
)
[1] => Array
(
[FullName] => Dvs Patel
[1] => 2
)
)
I have two array like this
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
echo "<pre>";
print_r($arr1);
print_r($arr2);
And I want output like this
Array
(
[0] => Array
(
[0] =>Prabhash
[1] =>9
)
[1] => => Array
(
[0] =>Nagda
[1] =>1
)
[2] => => Array
(
[0] =>Sayyed
[1] =>2
)
)
I have tried to combine and merge array but not success, Hope someone will help me for this better.
PHP code demo
<?php
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$result=array();
foreach($arr1 as $key => $value)
{
if(isset($result[$value]))
{
$result[$value][1]+=$arr2[$key];
}
else
{
$result[$value]=array($value,$arr2[$key]);
}
}
$result= array_values($result);
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)
Short solution using array_map, array_keys, array_flip, array_unique, array_intersect_key and array_sum functions:
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$result = array_map(function($n) use($arr1, $arr2){
$sum = array_sum(array_intersect_key($arr2, array_flip(array_keys($arr1, $n))));
return [$n, $sum];
}, array_unique($arr1));
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)
Try it.
<?php
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$newArray = array();
foreach($arr1 as $key => $value) {
$newArray[$value][0] =$value;
if(!isset($newArray[$value][1]) || $newArray[$value][1] == null)
$newArray[$value][1] = $arr2[$key];
else
$newArray[$value][1] = $newArray[$value][1]+$arr2[$key];
}
$newArray = array_values($newArray);
echo "<pre>";
print_r($newArray);
?>
OUTPUT :
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)
I have two arrays
$arr1=Array
(
[0] => Array
(
[0] => 'a'
),
[1]=>Array
(
[0]=>'b'
),
[2] => Array
(
[0] => 'c'
),
[3]=>Array
(
[0]=>'d'
),
[4]=>Array
(
[0]=>'e'
)
);
$arr2=array('1','2');
output should be
$arr3=Array
(
[0] => Array
(
[0] => 'a',
[1]=>'1'
),
[1]=>Array
(
[0]=>'b',
[1]=>'2'
),
[2] => Array
(
[0] => 'c',
[1]=>'1'
),
[3]=>Array
(
[0]=>'d',
[1]=>'2'
),
[4]=>Array
(
[0]=>'e',
[1]=>'1'
)
);
can someone please suggest me some solutions
You can do this with a MultipleIterator and attach the first array as ArrayIterator and the second one as InfiniteIterator, e.g.
<?php
$arr1 = [["a"], ["b"], ["c"], ["d"], ["e"]];
$arr2 = [1,2];
$result = [];
$mIt = new MultipleIterator();
$mIt->attachIterator(new ArrayIterator($arr1));
$mIt->attachIterator(new InfiniteIterator(new ArrayIterator($arr2)));
foreach($mIt as $v)
$result[] = array_merge($v[0], [$v[1]]);
print_r($result);
?>
This version will allow $arr2 to contain any number of values, should that be a requirement:
<?php
$arr1 = [
['a'], ['b'], ['c'], ['d'], ['e'],
];
$arr2 = ['1', '2'];
// wrap the array in an ArrayIterator and then in an
// InfiniteIterator - this allows you to continually
// loop over the array for as long as necessary
$iterator = new InfiniteIterator(new ArrayIterator($arr2));
$iterator->rewind(); // start at the beginning
// loop over each element by reference
// push the current value in `$arr2` into
// each element etc.
foreach ($arr1 as &$subArray) {
$subArray[] = $iterator->current();
$iterator->next();
}
print_r($arr1);
This yields:
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 1
)
[3] => Array
(
[0] => d
[1] => 2
)
[4] => Array
(
[0] => e
[1] => 1
)
)
Hope this helps :)
I'm looking to write a function which creates all permutation of a list of arrays (The list is dynamical). Now I found 2 articles, http://dannyherran.com/2011/06/finding-unique-array-combinations-with-php-permutations/ and Finding cartesian product with PHP associative arrays. But I don't want to store them as multiple arrays, I want to add each array to each possibility so I can use them later.
In fact I want to multiply each array with the other.
For example:
$array = array(
array(
1,
2
),
array(
'A',
'B',
'C'),
array(
'I',
'II')
);
In this form:
Array
(
[0] => Array
(
[0] => 1
[1] => Array
(
[0] => Array
(
[0] => A
[1] => Array
(
[0] => I
[1] => II
)
)
[1] => Array
(
[0] => B
[1] => Array
(
[0] => I
[1] => II
)
)
[2] => Array
(
[0] => C
[1] => Array
(
[0] => I
[1] => II
)
)
)
)
[1] => Array
(
[0] => 2
[1] => Array
(
[0] => Array
(
[0] => A
[1] => Array
(
[0] => I
[1] => II
)
)
[1] => Array
(
[0] => B
[1] => Array
(
[0] => I
[1] => II
)
)
[2] => Array
(
[0] => C
[1] => Array
(
[0] => I
[1] => II
)
)
)
)
)
I think this big example made my problem clear. For this type of array I created a function:
foreach ($array[1] as $value) {
$return1[] = array($value, $array[2]);
}
foreach ($array[0] as $value) {
$return[] = array($value, $return1);
}
print_r($return);
Now I want to create this function inside a recursive function (so it becomes dynamical) but I got stuck. I wanted to pass the amount of arrays to the function and then iterate.
function createTree($array, $loops=3){
$b = $array[$loops-2];
foreach ($b as $v) {
$return[] = array($v, createTree($return, $loops-1));
}
print_r($return);
}
Maybe there is a total other solution to multiply the arrays? But the function which isn't recursive is easy for me, but making it recursive...
Thanks for your help
function createTree($array){
switch(count($array)) {
case 0:
die('Illegal argument.');
case 1:
return $array[0];
default:
$lastArray = array_pop($array);
$subArray = createTree($array);
foreach ($lastArray as $item) {
$return[] = array($item, $subArray);
}
return $return;
}
}
var_dump(createTree(array_reverse($array)));
I have a big problem with a array and I want to group by key (or I don't now :( )
I get the links from mySQL and I do the following:
$lien2 = $links2['text'];
$lien2 = stripslashes($lien2);
$lien2 = htmlspecialchars($lien2);
$lien2 = nl2br($lien2);
preg_match_all('#http://.*?\.?([a-zA-Z0-9\-]+)(\.[a-zA-Z0-9]+)/[a-zA-Z0-9\/\*\-\?\&\%\=\,\.\;\#\_]+#i', $lien2, $lien2_result, PREG_SET_ORDER);
This is the array $lien2_result:
Array
(
[0] => Array
(
[0] => links1
[1] => A
)
[1] => Array
(
[0] => links2
[1] => B
)
)
Array
(
[0] => Array
(
[0] => links3
[1] => C
)
)
Array
(
[0] => Array
(
[0] => links4
[1] => B
)
)
Array
(
[0] => Array
(
[0] => links5
[1] => D
)
)
Array
(
[0] => Array
(
[0] => links6
[1] => E
)
)
and I want to get the following result:
A
links1
B
links2
links4
C
links3
D
links5
E
links6
I would adjust the query personally but if you are stuck with this resultset you could rewrite it with
foreach($lien2_result as $lien2){
foreach($lien2 as $item){
$arr[$item[1]][] = $item[0];
}
}
where print_r($arr) would result something like:
$arr = Array('A' => Array('links1'), 'B' => Array('links2','links4')); //and so on..
And actual printing the way you asked:
foreach($arr as $name => $value){
echo($name.'<br />');
foreach($value as $item){
echo($item.'<br />');
}
}
EDIT
Here's an example
http://sandbox.onlinephpfunctions.com/code/c0da893797cb2049e8346168b280a9f5b1fa145b