I have two array. I want to merge these two array with the same index.
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
And I want this after merge
Array
(
[0] => a
[1] => 1
[2] => b
[0] => 2
[1] => c
[2] => 3
)
in php you can do simply
$arr=array();
$arr_one=array('a','b','c');
$arr_two=array(1,2,3);
array_push($arr,$arr_one);
array_push($arr,$arr_two);
print_r($arr);
Related
I have 2 PHP arrays that look like this..
$array1
--------
Array
(
[0] => Array
(
[0] => 64
[1] => Apple
)
[1] => Array
(
[0] => 22
[1] => Pear
)
[2] => Array
(
[0] => 3
[1] => Raisin
)
[3] => Array
(
[0] => 15
[1] => Grape
)
[4] => Array
(
[0] => 11
[1] => Banana
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
$array2
--------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
)
I want to merge the arrays together but put the matching items from $array2 at the top so the result would look like this...
$array3
-------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
[2] => Array
(
[0] => 64
[1] => Apple
)
[3] => Array
(
[0] => 3
[1] => Raisin
)
[4] => Array
(
[0] => 15
[1] => Grape
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
I'm not sure how to approach, should I merge the two first and then try and do some ordering, or is there a more efficient approach?
Get the 2nd array and then a rest of the 1st array
array_merge($arr2, array_udiff($arr1, $arr2, function($i1, $i2) {return $i1[0]-$i2[0];}));
Below are my arrays echoed inside a for-each loop.
Array
(
[0] => 1
[1] => 2
[2] => 2
)
Array
(
[0] => 1
[1] => 2
[2] => 1
)
Array
(
[0] => 2
[1] => 1
[2] => 1
)
Array
(
[0] => 3
[1] => 3
[2] => 1
)
Array
(
[0] => 3
[1] => 3
[2] => 3
)
Array
(
[0] => 3
[1] => 3
[2] => 2
)
Array
(
[0] => 4
[1] => 2
[2] => 2
)
Array
(
[0] => 4
[1] => 2
[2] => 1
)
I would like to group these arrays based on the value of the first item (index = 0) and get the following dimension array.
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 2
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 1
)
)
Array
(
[3] => Array
(
[0] => 2
[1] => 1
[2] => 1
)
)
Array
(
[4] => Array
(
[0] => 3
[1] => 3
[2] => 1
)
[5] => Array
(
[0] => 3
[1] => 3
[2] => 3
)
[6] => Array
(
[0] => 3
[1] => 3
[2] => 2
)
)
Array
(
[7] => Array
(
[0] => 4
[1] => 2
[2] => 2
)
[8] => Array
(
[0] => 4
[1] => 2
[2] => 1
)
)
I have spent hours to figure this out but due to less experience I still cannot get this done. Please help me with some algoritme.
Won't that do the trick?
$result = [];
foreach ($originalArrays as $array) {
$result[$array[0]][] = $array;
}
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.
I am fetching some of the datas from my database and one of my column has multiple entries from one user say col(col1) values(a,b,c). Now when I am selecting data from this database, my array looks like this,
Array
(
[0] => srt
[1] => qwe
[2] => xyz
[3] => abc
[4] => 1
)
Array
(
[0] => srt
[1] => qwe
[2] => xyz
[3] => abc
[4] => 2
)
Array
(
[0] => srt
[1] => qwe
[2] => xyz
[3] => abc
[4] => 3
)
Now as my array shows only fourth element of array is different.
I want to insert this data into another table which has different columns for different values means only one row corresponding to one user. Thus
col(col1) turns in to col(cola,colb,colc).
Now I want to insert like
cola -> 1,colb -> 2,colc -> 3.
But I am not able to access these values. If I select array[4] then it selects all four elements if I try this $array=array($result[4]); then it create an array of result but with same index value
Array ( [0] => 1 )
Array ( [0] => 2 )
Array ( [0] => 3 )
Array ( [0] => 4 )
I hope I am able to clarify my question.
So please suggest me some way so that I can access these values in some way.
Thanks !
EDIT
my code
while($result= mysql_fetch_array($select))
{
echo "<pre>";
print_r($result);
$array=array($result[4]);
echo "</pre>";
print_r($array);
my result
Array
(
[0] => suresh.galaxy#gmail.com
[email] => suresh.galaxy#gmail.com
[1] => 2011-01-06 13:00:36
[date_joined] => 2011-01-06 13:00:36
[2] => 1
[is_active] => 1
[3] => 1
[attribute_id] => 1
[4] => suresh
[info] => suresh
)
Array ( [0] => suresh )
Array
(
[0] => suresh.galaxy#gmail.com
[email] => suresh.galaxy#gmail.com
[1] => 2011-01-06 13:00:36
[date_joined] => 2011-01-06 13:00:36
[2] => 1
[is_active] => 1
[3] => 2
[attribute_id] => 2
[4] => patidar
[info] => patidar
)
Array ( [0] => patidar )
Array
(
[0] => suresh.galaxy#gmail.com
[email] => suresh.galaxy#gmail.com
[1] => 2011-01-06 13:00:36
[date_joined] => 2011-01-06 13:00:36
[2] => 1
[is_active] => 1
[3] => 4
[attribute_id] => 4
[4] => e5c59af60000c8dff51e4e9a315ab152:vN
[info] => e5c59af60000c8dff51e4e9a315ab152:vN
)
Array ( [0] => e5c59af60000c8dff51e4e9a315ab152:vN )
You could make a loop first, then from there you could slice the array after the loop
<?php
$arr = array();
while($result= mysql_fetch_array($select))
{
$arr[] = $result;
}
print_r($arr);
echo $arr[0][4] ;
My array looks like this:
Array
(
[0] => Array
(
[0] => 1
[1] => 6
[2] => 4
[3] => 5
)
[1] => Array
(
[0] => 272.05
[1] => 63.54
[2] => 544.79
[3] => 190.62
)
[2] => Array
(
[0] => 2011-03-06 14:08:19
[1] => 2011-03-06 14:29:04
[2] => 2011-03-06 14:28:39
[3] => 2011-03-06 14:29:28
)
)
I want to sort by $myArray[1]. I have this usort function:
function sortAmount($a, $b) {
return strnatcmp($a[1], $b[1]);
}
It is called like this:
usort($myArray, "sortAmount");
However, the array does not change after calling usort. I want the numbers in $myArray[1] to be sorted in ascending order, and for the corresponding indexes in $myArray[0] and $myArray[2] to change with it.
I think you want array_multisort:
array_multisort($a[1], $a[0], $a[2]);
gives
Array
(
[0] => Array
(
[0] => 6
[1] => 5
[2] => 1
[3] => 4
)
[1] => Array
(
[0] => 63.54
[1] => 190.62
[2] => 272.05
[3] => 544.79
)
[2] => Array
(
[0] => 2011-03-06 14:29:04
[1] => 2011-03-06 14:29:28
[2] => 2011-03-06 14:08:19
[3] => 2011-03-06 14:28:39
)
)
Apart from that, why do you use strcmp to compare numbers?