I have two arrays:
print_r($array_one):
Array
(
[a] => Array (
[param1] => 1
[param2] => 2
)
[b] => Array (
[param1] => 3
[param2] => 4
)
)
print_r($array_two):
Array
(
[param3] => 5
)
I want to form an array that looks like:
Array
(
Array
(
[a] => Array (
[param1] => 1
[param2] => 2
)
[b] => Array (
[param1] => 3
[param2] => 4
)
)
Array
(
[param3] => 5
)
)
But both array_merge() and array_merge_recursive() will create:
Array
(
[a] => Array (
[param1] => 1
[param2] => 2
)
[b] => Array (
[param1] => 3
[param2] => 4
)
[param3] => 5
)
The original array was intended for a json encode to make a dimensional json file, but the result of merge of arrays is not what I want.
Would someone please point out how to make it work?
Couldn't you just do something like:
$new_array = array();
$new_array[] = $array_one;
$new_array[] = $array_two;
Something like $newArray = array($array1, $array2); ?
If I am reading this right, this is what you are looking for:
$output = array($array_one, $array_two);
Related
What is the most efficient way in initializing large amount of multidimensional array in PHP?
example: I'm going to create 100 Multidimensional array that look like this:
Array
(
[1] => Array
(
[multi] => 1
)
[2] => Array
(
[multi] => 2
)
[3] => Array
(
[multi] => 3
)
[4] => Array
(
[multi] => 4
)
[5] => Array
(
[multi] => 5
)
.......
)
Currently, I'm using this code to create the array shown above:
// 100 arrays
for($i=1; $i<=100; $i++){
$array[$i]['multi']=$i;
}
I also found an alternative way by using array_fill() and array_fill_keys(), but It only allows the same value to be initialized in an array:
$array = array_fill(1, 100, array_fill_keys(array("multi"), "value_here"));
QUESTION: Is there a more efficient way in initializing this kind of array in terms of speed?
You could use array_map over the range of values you want:
$array = array_map(function ($v) { return array('multi' => $v); }, range(0, 5));
print_r($array);
Output:
Array
(
[0] => Array
(
[multi] => 0
)
[1] => Array
(
[multi] => 1
)
[2] => Array
(
[multi] => 2
)
[3] => Array
(
[multi] => 3
)
[4] => Array
(
[multi] => 4
)
[5] => Array
(
[multi] => 5
)
)
If you don't want the 0 element, just unset($array[0]);
Demo on 3v4l.org
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 2 arrays as below. I want to remove data from array2 if array1 has the stu_id. final array should be like result_array.
$array1 = Array
(
[0] => Array
(
[stu_id] => 1
[name] => mr.a
)
[1] => Array
(
[stu_id] => 3
[name] => mr.b
)
)
$array2 = Array
(
[0] => Array
(
[id] => 1
[stu_id] => 1
[data] => abc
)
[1] => Array
(
[id] => 2
[stu_id] => 2
[data] => xyz
)
[3] => Array
(
[id] => 3
[stu_id] => 3
[data] => aaa
)
)
$result_array = Array
(
[0] => Array
(
[id] => 2
[stu_id] => 2
[data] => xyz
)
)
I tried array_diff, $result_array = array_diff($array2, $array1); but it's not working.
Please help me to do this.
Temporarily assign keys using array_column() with stud_id (NULL retains the full subarray data), then use array_diff_key() to filter, and array_values() to reset the keys:
Code: (Demo)
$array1=[
['stu_id'=>1,'name'=>'mr.a'],
['stu_id'=>3,'name'=>'mr.b']
];
$array2=[
['id'=>1,'stu_id'=>1,'data'=>'abc'],
['id'=>2,'stu_id'=>2,'data'=>'xyz'],
['id'=>3,'stu_id'=>3,'data'=>'aaa']
];
//var_export(array_column($array1,NULL,'stu_id'));
//var_export(array_column($array2,NULL,'stu_id'));
var_export(array_values(array_diff_key(array_column($array2,NULL,'stu_id'),array_column($array1,NULL,'stu_id'))));
Output:
array (
0 =>
array (
'id' => 2,
'stu_id' => 2,
'data' => 'xyz',
),
)
If you'd like to use a foreach loop structure, generate a filtering array of stu_id's from $array1 and write a conditional check on each iteration of $array2. This method doesn't not modify the original arrays, so they can be reused "down script".
Code:
$stu_ids=array_column($array1,'stu_id');
foreach($array2 as $row){
if(!in_array($row['stu_id'],$stu_ids)){
$result[]=$row; // auto-index the qualifying subarrays
}
}
var_export($result);
// same result as above method
foreach($array1 as $data1){
foreach($array2 as $k => $data2){
if($data2["stu_id"] == $data1["stu_id"]){
unset($array2[$k]);
break;
}
}
}
$result_array = $array2;
Need help from the team,
I have this scenario of having 2 identical keys in each array with different values, i want them to be merged into one key were the values also are in it
example:
arrayData1(
[2] => Array
(
[EXP1] => Array (records...)
[EXP2] => Array (records...)
)
)
arrayData2(
[2] => Array
(
[EXP3] => Array (records...)
[EXP4] => Array (records...)
)
)
Having the output like this:
arrayFinal (
[2] => Array
(
[EXP1] => Array (records...)
[EXP2] => Array (records...)
[EXP3] => Array (records...)
[EXP3] => Array (records...)
)
)
Thanks!
First of all you cannot have two same keys in a single array, what you can do is use the array_merge_recursive function in php to merge both the arrays, and the repeating keys will have a new array with all the repeating key values..
$array1 = [
'EXP1' => [1,2,3],
'EXP2' => [2,3,4]
];
$array2 = [
'EXP2' => [5,6,7],
'EXP3' => [8,9,10]
];
Now there are two EXP2 keys, so when you use array_merge_recursive() you get something like this,
print_r(array_merge_recursive($array1, $array2));
//output Array (
[EXP1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[EXP2] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
)
[EXP3] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
I know that ksort() is used to sort array by keys. The max dimension of my array always differs. Sometime 2, sometimes 5, sometime 10 dimensional so how do I use ksort() to do sorting dynamically without knowing how many dimensions exist in it?
Thanks
EXAMPLE ARRAY
[2010] => Array
(
[3] => Array
(
[B] => Array
(
[6] => Array
(
[Patato] =>
)
[C] => Array
(
[Patato] =>
[Zozo] =>
)
)
[A] => Array
(
[F] => Array
(
[Tomato] =>
[Apple] =>
[Kiwi] =>
)
)
[1] => Array
(
[4] => Array
(
[A] => Array
(
[Orange] =>
)
)
If you mean you want to sort recursive:
function deep_ksort(&$arr) {
ksort($arr);
foreach ($arr as &$a) {
if (is_array($a) && !empty($a)) {
deep_ksort($a);
}
}
}
This comes from http://www.php.net/manual/en/function.ksort.php#105399