How to set index key automatically in multidimensional array - php

This is my old array.
$oldarray = Array
(
[0] => http://test.to/getac/l4p0y6ziqt9h
[mock] => stdClass Object
(
[0] => http://test.to/getae/vidzichawal1
[1] => http://test.to/getae/vidzi6
[4] => http://test.to/getae/1x5fbr9t64xn
[2] => http://test.to/getae/vidzi7
)
)
which i want to merge with this new array:
$newarray = Array
(
[mock] => Array
(
[0] => http://test.to/getae/vidzichawal2
)
)
I am merging array by array_merge_recursive($oldarray, $newarray);
And the Result is this:
Array
(
[0] => http://test.to/getac/l4p0y6ziqt9h
[mock] => Array
(
[0] => http://test.to/getae/vidzi5
[1] => http://test.to/getae/vidzi6
[4] => http://test.to/getae/1x5fbr9t64xn
[2] => http://test.to/getae/vidzi7
[0] => http://test.to/getae/vidzichawal1
)
);
All things is working good but there is one problem you can see in result there double 0 key when i am using this link in loop only 1 link retrive of 0 i want to set this keys automatically like 0 1 2 3 4 5 6 and go on after merging.
I hope you understand what i want thanks

Use array_merge()
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
The above example will output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
REF.
Note You can't have duplicate keys!
UPDATE
Example using array_merge_recursive()
<?php
$oldarray = array('http://test.to/getac/l4p0y6ziqt9h', 'mock' => array('http://test.to/getae/vidzichawal1', 'http://test.to/getae/vidzi6', 'http://test.to/getae/1x5fbr9t64xn', 'http://test.to/getae/vidzi7'));
$newarray = array('mock' => array('http://test.to/getae/vidzichawal2'));
$result = array_merge_recursive($oldarray, $newarray);
var_dump($result);
?>
OUTPUT
array (size=2)
0 => string 'http://test.to/getac/l4p0y6ziqt9h' (length=33)
'mock' =>
array (size=5)
0 => string 'http://test.to/getae/vidzichawal1' (length=33)
1 => string 'http://test.to/getae/vidzi6' (length=27)
2 => string 'http://test.to/getae/1x5fbr9t64xn' (length=33)
3 => string 'http://test.to/getae/vidzi7' (length=27)
4 => string 'http://test.to/getae/vidzichawal2' (length=32)

Related

add two multidimensional array and add all items of them in another array

I have two multidimensional arrays and want to join them with all the items.
it can be re-index again wont be any issue.
I have create these array in a foreach loop and use this for batch insert query.
here is the array, I want to add both of them
Array
(
[1] => Array
(
[0] => Array
(
[question_def_id] => 1
[description] => 3
[description_hindi] => 3
[correct] => 0
)
[1] => Array
(
[question_def_id] => 1
[description] => 4
[description_hindi] => 4
[correct] => 1
)
[2] => Array
(
[question_def_id] => 1
[description] => 5
[description_hindi] => 5
[correct] => 0
)
[3] => Array
(
[question_def_id] => 1
[description] => 6
[description_hindi] => 6
[correct] => 0
)
)
)
Join two multidimensional array
$test_array1 = array(array('question_def_id'=>1,'description'=>2), array('question_def_id'=>3,'description'=>4));
$test_array2 = array(array('question_def_id'=>5,'description'=>6), array('question_def_id'=>7,'description'=>8));
$C = array_merge($test_array1, $test_array2);
print_r($C);
You can use PHP array_merge_recursive function to merge both arrays into one.
See here
E.g
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>

Sort multidimensional array by multidimensional array value

I want to use the 'id' array of Array 1 to order all other sibling arrays in Array 2. The 'id' array is supposed to act as the schema for all other arrays in $array1 and $array2.
This is easier explained with an example.
I have the following array, Array 1 :
Array
(
[id] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[headline] => Array
(
[0] => test 1
[1] => test 3
[2] => test 2
)
)
And another array, Array 2:
Array
(
[id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[headline] => Array
(
[0] => tc test 1
[1] => tc test 2
[2] => tc test 3
)
)
That I would like to look like the following:
Array
(
[id] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[headline] => Array
(
[0] => tc test 1
[1] => tc test 3
[2] => tc test 2
)
)
Right now I'm trying to force it with foreach statements (and failing), but there has to be a better way.
This is a “fake” sort, because I fill an empty array through a foreach() loop, then I replace original array:
$array1 = [ 'id' => [ 1,3,2 ], 'headline' => [ 'test 1','test 3','test 2' ] ];
$array2 = [ 'id' => [ 1,2,3 ], 'headline' => [ 'test 1','test 2','test 3' ] ];
$result = array();
foreach( $array1['id'] as $val )
{
$key = array_search( $val, $array2['id'] );
$result['id'][] = $array2['id'][$key];
$result['headline'][] = $array2['headline'][$key];
}
$array2 = $result;
print_r( $array2 );
Output:
Array
(
[id] => Array
(
[0] => 1
[1] => 3
[2] => 2
)
[headline] => Array
(
[0] => test 1
[1] => test 3
[2] => test 2
)
)
I execute a foreach loop through criterion 'id' array, find each value in array to be sorted, retrieve its key and use it to add values to resulting array.
Not so proud of this solution, but this is.

Merge multidimensional array preserving keys [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 10 months ago.
I have three arrays as follows:
Array
(
[1000] => Item 0
[1001] => Item 1
[1002] => Item 2
)
Array
(
[1000] => £35.00
[1001] => £60.00
[1002] => £24.00
)
Array
(
[1000] => 1
[1001] => 2
[1002] => 3
)
I need to merge these three arrays preserving the keys as follows:
Array
(
[1000] => Array
(
[0] => Item 0
[1] => £35.00
[2] => 1
)
[1001] => Array
(
[0] => Item 1
[1] => £60.00
[2] => 2
)
[1002] => Array
(
[0] => Item 2
[1] => £24.00
[2] => 3
)
)
array_map(null, array1, array2, array3) solves it to some level but doesn't preserves the keys. How can it be done?
You could wrap your array_map together with an array_keys() against your original array within an array_combine()
$array1 = array(
1000 => 'Item 0',
1001 => 'Item 1',
1002 => 'Item 2',
);
$array2 = array(
1000 => '£35.00',
1001 => '£60.00',
1002 => '£24.00',
);
$array3 = array(
1000 => 1,
1001 => 2,
1002 => 3,
);
$result = array_combine(
array_keys($array1),
array_map(null, $array1, $array2, $array3)
);
var_dump($result);
Try this code, save one array keys in another array and fill keys after merged array
$array1_keys = array_keys($array1);
$mapped_array = array_map(null, $array1, $array2, $array3);
//now assign original keys
$merged_final_array = array_fill_keys($array1_keys, $mapped_array);
DEMO
I hope this code will help you:
$one =Array( "1000" => "£35.00","1001" => "£60.00","1002" => "£24.00","1003" => "£36.00","1004" => "£80.00","1005" => "£24.00");
$two = array("1000"=>"1","1001"=>"2","1002"=>"3","1003"=>"4","1004"=>"5","1005"=>"6");
$response = array();
foreach(array_keys($one) as $key =>$val)
{
$response[$val] = array(0=>"item ".$key,1=>$one[$val],2=>$two[$val]);
}
echo "<pre>";print_R($response);

Transforming array values in elements of a subarray using PHP

I would like to merge two arrays in a way that values of first array are transformed in first elements of the subarray and values of the second array are transformed in second elements of the subarray. Here my example:
Array01
(
[0] => 41558194
[1] => 44677841
[2] => 44503689
[3] => 40651770
)
Array02
(
[0] => 551
[1] => 546
[2] => 531
[3] => 519
)
mergedArray
(
[0] => Array([0] => 41558194 [1] => 551)
[1] => Array([0] => 44677841 [1] => 546)
[2] => Array([0] => 44503689 [1] => 531)
[3] => Array([0] => 40651770 [1] => 519)
)
What is the most efficient way to do this?
Many thanks in advance!
Here is a concise example using array_map:
function merge_arrays($a1, $a2) {
return array($a1, $a2);
}
$result = array_map("merge_arrays", $arr, $arr2);
An example with your values :
$array1 = array(
0 => 41558194,
1 => 44677841,
2 => 44503689,
3 => 40651770
);
$array2 = array(
0 => 551,
1 => 546,
2 => 531,
3 => 519
);
$finalArray = array();
foreach ($array1 as $key1 => $value1) {
$finalArray[$key1] = array($value1, $array2[$key1]);
}

Remove duplicate elements off a multi-dimension array

I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke

Categories