I'm having two arrays in PHP as follows:
Array1 ( [1] => Array ( [0] => 16 )
[2] => Array ( [0] => 17 [1] => 29 )
[3] => Array ( [0] => 30 [1] => 31 )
) Total Element Count: 5
Array2 ( [1] => Array ( [0] => 21 )
[2] => Array ( [0] => 22 )
[3] => Array ( [0] => 23 )
[4] => Array ( [0] => 24 [1] => 25 )
[5] => Array ( [0] => 43 [1] => 44 )
) Total Element Count: 7
I want to pair above two arrays depending on count of first array, that means, first five elements of Array2 should get mixed with Array1 with outer 1D keys remaining intact.
Output should be as follows:
Output Array( [1] => Array ( [0] => 16 [1] => 21)
[2] => Array ( [0] => 17 [1] => 29 [2] => 22)
[3] => Array ( [0] => 30 [1] => 31 [2] => 23 )
[4] => Array ( [0] => 24 [1] => 25 )
)
If you want to avoid E_STRICT warnings:
function combine(array $arr1, array $arr2) {
$ret = array();
foreach ($arr1 as $k => $v) {
if (!array_key_exists($k, $ret)) {
$ret[$k] = array();
}
$ret[$k][] = $v;
}
foreach ($arr2 as $k => $v) {
if (!array_key_exists($k, $ret)) {
$ret[$k] = array();
}
$ret[$k][] = $v;
}
return $ret;
}
If you prefer a shorter version:
function combine(array $arr1, array $arr2) {
$ret = array();
foreach ($arr1 as $k => $v) {
$ret[$k][] = $v;
}
foreach ($arr2 as $k => $v) {
$ret[$k][] = $v;
}
return $ret;
}
What about http://www.php.net/manual/en/function.array-merge-recursive.php :P
Related
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 5 months ago.
here: Transforming array values in elements of a subarray using PHP I asked quite the same, but the arrays were flatter. I tried to adapt the code, but unfortunately without success.
How could I merge following arrays so the second array won't be added after the end of the first array, but each subarray of the first array will receive the correspondent values of subarrays of the second. In other words, I want to merge the subarrays. Here my example:
Array 01:
Array01 (
[0] => Array
(
[0] => 40292633
[1] => 412
)
[1] => Array
(
[0] => 41785603
[1] => 382
)
[2] => Array
(
[0] => 48792980
[1] => 373
)
[3] => Array
(
[0] => 44741143
[1] => 329
))
Array 02:
Array02(
[0] => Array
(
[0] => 3460581
[1] => 1407424B1
[2] => 951753
)
[1] => Array
(
[0] => 3484251
[1] => 1028325B1
[2] => 159357
)
[2] => Array
(
[0] => 3519102
[1] => 0586365A1
[2] => 456654
)
[3] => Array
(
[0] => 3529714
[1] => 1059876A1
[2] => 852258
))
Final array:
finalArray(
[0] => Array
(
[0] => 40292633
[1] => 412
[2] => 3460581
[3] => 1407424B1
[4] => 951753
)
[1] => Array
(
[0] => 41785603
[1] => 382
[2] => 3484251
[3] => 1028325B1
[4] => 159357
)
[2] => Array
(
[0] => 48792980
[1] => 373
[2] => 3519102
[3] => 0586365A1
[4] => 456654
)
[3] => Array
(
[0] => 44741143
[1] => 329
[2] => 3529714
[3] => 1059876A1
[4] => 852258
))
Many thanks in advance!
try this code
function merge_arrays($a1, $a2) {
return array($a1, $a2);
}
$result = array_map("merge_arrays", $a1, $a2);
foreach($result as $nr)
{
$nres[] = array_merge ($nr[0], $nr[1]);
}
Try this:
$result = array();
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
foreach($keys as $key) {
if( !array_key_exists($key, $arr1) ) {
$result[$key] = $arr2;
} else if( !array_key_exists($key, $arr2) ) {
$result[$key] = $arr1;
} else {
$result[$key] = array_merge($arr1[$key], $arr2[$key]);
}
}
It does not assume that both arrays have the same keys.
If you'd like to use array_map, this is an equivalent solution:
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
$result = array_map(function($key) use ($arr1,$arr2) {
if( !array_key_exists($key, $arr1) ) {
return $arr2;
} else if( !array_key_exists($key, $arr2) ) {
return $arr1;
}
return array_merge($arr1[$key], $arr2[$key]);
},
$keys);
If you're sure both arrays have the same keys, you can try this:
$result = array();
foreach($arr1 as $key => $arr1val) {
$result[$key] = array_merge($arr1val, $arr2[$key]);
}
I have an array a bit like:
Array (
[1] => Array
(
[1] => 21
[2] => 3
[0] => Analyst
)
[2] => Array
(
[1] => 47
[2] => 8
[0] => Catalysis
)
[3] => Array
(
[1] => 1
[2] => 0
[0] => Biomaterials
)
[4] => Array
(
[3] => 12
[4] => 2
[0] => Analyst
)
[5] => Array
(
[5] => 12
[6] => 2
[0] => Analyst
)
...
However I would like to renumber those entries with the same [0] value so that I end up with
[1] => Array
(
[1] => 21
[2] => 3
[3] => 12
[4] => 2
[5] => 12
[6] => 2
[0] => Analyst
)
So far I've tried getting the [0] values out of the $results array by putting them in their own array and saying if you're already there then add [3] and [4] to where [1] and [2] are in a new array but it's not working.
$final = array();
$jArray = array();
foreach($results as $key => $result) {
if(!in_array($result[0],$jArray) && !empty($result[0])) {
$jArray[$i] = $result[0];
$i++;
}
}
for($x = 0; $x < count($results); $x++) {
$k = array_search($results[$x][0],$jArray);
if(!isset($results[$x][1]))
$final[$k][1] = $results[$x][1];
if(!isset($results[$x][2]))
$final[$k][2] = $results[$x][2];
if(!isset($results[$x][3]))
$final[$k][3] = $results[$x][3];
if(!isset($results[$x][4]))
$final[$k][4] = $results[$x][4];
if(!isset($results[$x][5]))
$final[$k][5] = $results[$x][5];
if(!isset($results[$x][6]))
$final[$k][6] = $results[$x][6];
}
Any simpler ways of doing this?
You can do this way...
$new_arr=array();
$arkeys = array_unique(array_map(function ($v){ return $v[0];},$arr));
foreach($arr as $k=>$arr1)
{
$new_arr[$arr1[0]][]=array_slice($arr1,0,count($arr1)-1);
}
foreach($arkeys as $v)
{
$new_arr[$v] = call_user_func_array('array_merge', $new_arr[$v]);
}
print_r($new_arr);
OUTPUT :
Array
(
[Analyst] => Array
(
[0] => 21
[1] => 3
[2] => 12
[3] => 2
[4] => 12
[5] => 2
)
[Catalysis] => Array
(
[0] => 47
[1] => 8
)
[Biomaterials] => Array
(
[0] => 1
[1] => 0
)
)
Working Demo
If you just want to group by the first element of the sub array, a single loop is enough:
$result = array();
foreach ($array as $sub_arr) {
$key = $sub_arr[0];
unset($sub_arr[0]);
if (!isset($result[$key])) {
$result[$key] = array();
}
$result[$key] += $sub_arr;
}
Here
$final = array();
foreach($results as $key => $result) {
if (!in_array($result[0], array_keys( $final ) ) && !empty($result[0])) {
$final[$result[0]] = array( $result[0] );
}
foreach($result as $k => $v) {
if ($k != 0 && isset($v)) {
$final[$result[0]][$k] = $v;
}
}
}
How to arrange this array by last inner index ( 0, 1, 2 ) and get the value of the last inner index as the value of each second index:
Array
(
[text] => Array
(
[grid] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[image] => Array
(
[0] =>
[1] =>
[2] =>
)
[align] => Array
(
[0] => left
[1] => right
[2] => left
)
[title] => Array
(
[0] =>
[1] =>
[2] =>
)
[content] => Array
(
[0] =>
[1] =>
[2] =>
)
)
)
And have the results as below:
Array
(
[text] => Array
(
[0] => Array
(
[grid] => 3
[image] =>
[align] => left
[title] =>
[content] =>
)
[1] => Array
(
[grid] => 4
[image] =>
[align] => right
[title] =>
[content] =>
)
[2] => Array
(
[grid] => 5
[image] =>
[align] => left
[title] =>
[content] =>
)
)
)
This will do the work
function restructure($arr){
$newArr = array();
foreach($arr as $k => $v){
foreach($v as $k1 => $v1){
foreach($v1 as $k2 => $v2){
$newArr[$k][$k2][$k1] = $v2;
}
}
}
return $newArr;
}
As SiGanteng suggested, i dont see other ways than a for/foreach loop:
function buildArray($source, $key = false)
{
// Build the new array
$new_array = array();
// Define groups
$groups = $key === false ? array_keys($source) : array($key);
foreach($groups AS $group)
{
// Get the keys
$keys = array_keys($array[$group]);
// Count the values
$num_entries = count($array[$group][$keys[0]]);
for($i = 0; $i < $num_entries; $i++)
{
foreach($keys AS $key)
{
$new_array[$group][$i][$key] = $array[$group][$key][$i];
}
}
}
return $new_array;
}
This allow you to define the key you need to build; If not specified, the function build the array for every key.
This should work.
function formatit($arr) {
$new = array();
foreach($arr as $k=>$v) {
foreach($v as $k1=>$v1) {
$new[$k1][$k] = $v1;
}
}
return $new;
}
Tested. Call it as
$arr['text'] = formatit($arr['text']);
http://ideone.com/rPzuR
I have an array $matrix_part, containing arrays, and I want to rekey the inner keys to start at 1.
I am trying to code below but it doesn't work - it just stores the new array identically.
$temp_matrix = array();
foreach ($matrix_part as $k => $v){
$temp_matrix[$k++] = $v;
}
$matrix_part = $temp_matrix;
Source array:
Array
(
[0] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
[1] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
[2] => Array
(
[0] => 163
[1] => 23
[2] => 97
)
)
Desired output:
Array
(
[0] => Array
(
[1] => 163
[2] => 23
[3] => 97
)
[1] => Array
(
[1] => 163
[2] => 23
[3] => 97
)
[2] => Array
(
[1] => 163
[1] => 23
[3] => 97
)
)
Try use:
instead of this:
$temp_matrix[$k++] = $v;
do this:
$temp_matrix[++$k] = $v;
This maybe?
$input = array(
array(163, 23, 97),
array(163, 23, 97),
array(163, 23, 97),
);
$output = array_map(function ($innerArray) {
return array_combine(range(1, sizeof($innerArray)), $innerArray);
}, $input);
print_r($output);
foreach ($a as $outer_k => $outer_v) {
for ($i = count($outer_v) - 1; $i >= 0; $i--) {
$outer_v[$i+1] = $outer_v[$i];
}
unset($outer_v[0]);
$a[$outer_k] = $outer_v;
}
where $a is your input array
Could do something like ...
foreach ($matrix as $k=>$v) {
foreach ($v as $k2=>$v2) {
$tmp_arr[$k][$k2+1] = $v2;
}
}
$matrix = $tmp_arr;
I have this array:
Array
(
[0] => Array
(
[tag_id] => 1
)
[2] => Array
(
[tag_id] => 3
)
[22] => Array
(
[tag_id] => 44
)
[23] => Array
(
[tag_id] => 45
)
[25] => Array
(
[tag_id] => 47
)
[26] => Array
(
[tag_id] => 48
)
)
I'd like it to look something like this so its simpler for me to loop through and insert each value into a database:
Array
(
[0] => 1
[1] => 3
[2] => 44
[3] => 45
[4] => 47
[5] => 48
)
You can use array_map.
PHP 5.3 or higher:
$callback = function($value) {
return $value['tag_id'];
};
$result = array_map($callback, $array);
Below 5.3:
function collapseTagIds($value) {
return $value['tag_id'];
}
$result = array_map('collapseTagIds', $array);
Well, you could do this:
$new_array = array();
foreach($array as $key => $value)
{
$new_array[$key] = $value['tag_id'];
}
print_r($new_array);
In your case, you have just one index on $value. If you don't want to specify the index name just do it:
$new_array = array();
foreach($array as $key => $value) {
$new_array[$key] = reset($value);
}
print_r($new_array);