i can't believe that i can't find a solution to this seemingly simple problem, and i tried everything.
i have two multi arrays:
the first one:
Array
(
[3] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
)
)
[4] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
)
the second one has the exact same structure, but with new data:
Array
(
[3] => Array
(
[1] => Array
(
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[final_approved] =>
[final_id_note] => 19
)
)
)
what i need mergin both multi-arrays is this result:
Array
(
[3] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
[final_approved] =>
[final_id_note] => 19
)
)
[4] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
)
but i can't get this result with any common php function:
array_merge:
Array
(
[0] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
)
)
[1] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
[2] => Array
(
[1] => Array
(
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[final_approved] =>
[final_id_note] => 19
)
)
)
it replaces with new indexes.
array_push:
Array
(
[3] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
)
)
[4] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
[5] => Array
(
[3] => Array
(
[1] => Array
(
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[final_approved] =>
[final_id_note] => 19
)
)
)
)
it literally pushes in with the second array with new indexes, which is correct of course lol
i guess that the answer is pretty simple, but i already googled and try everything that my newbie ability can.
can anyone give me some advice?
thanks!
There are recursive array functions in PHP. array_merge_recursive, despite the promising name, will not work here, as it will reindex the numeric keys and add overlapping the data to the end.
However, there is array_replace_recursive that does exactly what you want when you have a multidimensional array with significant keys. Here's your data (in a usable format!):
// The base data:
$a_one = [
3 => [
1 => [
'approved' => 1,
'id_note' => 1,
'surname' => 'Rawson',
],
2 => [
'approved' => 0,
'id_note' => 18,
'surname' => 'Vallejo',
],
],
4 => [
1 => [
'school_id' => 1,
],
2 => [
'available' => 1,
],
],
];
// Data to be added by matching keys:
$a_two = [
3 => [
1 => [
'final_approved' => 1,
'final_id_note' => 1,
],
2 => [
'final_approved' => 0,
'final_id_note' => 19,
],
],
];
Then let's mash them together:
$merged = array_replace_recursive($a_one, $a_two);
This results in the following "merged" array:
[
3 => [
1 => [
'approved' => 1,
'id_note' => 1,
'surname' => 'Rawson',
'final_approved' => 1,
'final_id_note' => 1,
],
2 => [
'approved' => 0,
'id_note' => 18,
'surname' => 'Vallejo',
'final_approved' => 0,
'final_id_note' => 19,
],
],
4 => [
1 => [
'school_id' => 1,
],
2 => [
'available' => 1,
],
],
]
In summary, the way this function behaves (from the manual, numbering added):
If a key from the first array exists in the second array, its value will be replaced by the value from the second array.
If the key exists in the second array, and not the first, it will be created in the first array.
If a key only exists in the first array, it will be left as is.
Or in a more compact statement:
key: scalar EXISTS in A, B => REPLACE from B to A
key: scalar EXISTS in B => CREATE in A
key: scalar EXISTS in A => LEAVE in A
Again, the manual says:
When the value in the first array is scalar, it will be replaced by the value in the second array, may it be scalar or array. When the value in the first array and the second array are both arrays, array_replace_recursive() will replace their respective value recursively.
What's important to note here is that array members with matching keys, that are arrays, will not be "replaced" in full. The "replacement" only applies to the final scalar values or "leaves" of the array (per the logic above). The arrays will be "merged", ie. non-matching keys with scalar values will be combined within each "final array" (or "leafy array" for lack of a better term).
N.B. If you're using this function to "merge" more complex arrays, you'll want to double-check that the outcome matches your expectations. I can't remember off the top of my head all the "quirks" that exist in using this function, but I assure you they are there... don't assume that it will create a seamless union of any and all datasets you may throw at it.
Related
I have a Array list as below, how to split them into equal parts, sort equal parts by id, and merge equal parts in a id using PHP?
Array
(
[0] => Array
(
[id] => 121
[owner] => xa
[name] => xjs
)
[1] => Array
(
[id] => 139
[owner] => xa
[name] => xjs
)
[2] => Array
(
[id] => 1456
[owner] => xv
[name] => bjs
)
[3] => Array
(
[id] => 1896
[owner] => xb
[name] => bjs
)
[4] => Array
(
[id] => 1963
[owner] => xb
[name] => bjs
)
)
Supposed I would like to split them into 2 equal items,
Split them firstly, and the smallest id should be in a equal part at first, they should be like this:
Array
(
[0] => Array
(
[id] => 121
[owner] => xa
[name] => xjs
)
[1] => Array
(
[id] => 139
[owner] => xa
[name] => xjs
)
)
Array
(
[0] => Array
(
[id] => 1456
[owner] => xv
[name] => bjs
)
[1] => Array
(
[id] => 1896
[owner] => xb
[name] => bjs
)
)
Array
(
[0] => Array
(
[id] => 1963
[owner] => xb
[name] => bjs
)
)
my sample code is 5 items, and if 6 items, it should be [1,2],[3,4],[5,6], how many items or parts are not sure, but i should be 2 equal pieces.
Merge them secondly(This is our logic of our project, if you don't know what I am talking about, please ignore the merge question, I only need to know how to split them):
[121, 139] => merge into 139,
[1456, 1896] => merge into 1896,
new list: [139, 1896] => merge into 1896,
and the final list [1896, 1963] merge into the final id 1963
Sounds like you wanted something like this:
$array = [
[
'id' => 121,
'owner' => 'xa',
'name' => 'xjs',
],
[
'id' => 139,
'owner' => 'xa',
'name' => 'xjs',
],
[
'id' => 1456,
'owner' => 'xv',
'name' => 'bjs',
],
[
'id' => 1896,
'owner' => 'xb',
'name' => 'bjs',
],
[
'id' => 1963,
'owner' => 'xb',
'name' => 'bjs',
]
];
// custom function to compare which ID is greater
function customCompare($a, $b)
{
if ($a['id'] === $b['id']) {
return 0;
}
return ($a['id'] < $b['id']) ? -1 : 1;
}
// sort the whole array
usort($array, "customCompare");
// print the whole array as pairs,
// if there is an unpair number
// the last one will be a single member
print_r(array_chunk($array, 2));
Can you please explain how to change one array value to second value.
Please find below example.
First array
Array
(
[20239802] => one test
[20239801] => two testttttt
)
Second array
Array (
[content] => Array (
[0] => Array (
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239802
[track] => Lede Inthati Santhosham
[duration] => 0
)
[1] => Array (
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239801
[track] => Lede Inthati Santhosham
[duration] => 0
)
)
[autoshuffle_pack] => no
)
We need to replace [track] value in second array if match first array [20239802] with second array [content_id]
Need out put:-
Array
(
[content] => Array
(
[0] => Array
(
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239802
[track] => one test
[duration] => 0
)
[1] => Array
(
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239801
[track] => two testttttt
[duration] => 0
)
)
[autoshuffle_pack] => no
)
Check [track] value change in my need out put
as per depend first array.
Array
(
[20239802] => one test
[20239801] => two testttttt
)
with second array
[content_id] => 20239801
[track] => two testttttt
Is this you want to do :
foreach($second_array as $key => $second_row) {
$content_id = $second_row['content_id'];
if(isset($first_array[$content_id]) && $first_array[$content_id] != '') {
$second_array['track'] = $first_array[$content_id];
}
}
You can do you own function.
Foreach records in your first array:
Loop though the second and search for key == content_id
If it matched, set the value of array2[$index][track] to array1[key]
If not just continue
You can also use array_search() function to find the match faster, for I'll let you take a look at the PHP documentation
Given the following arrays how can I elegantly validate that option, price and cost arrays have matching key values?
Array
(
[option] => Array
(
[1] => C
[2] => M
[3] => G
)
[price] => Array
(
[1] => 100
[2] => 200
[3] => 300
)
[cost] => Array
(
[1] => 0
[2] => 0
[3] => 0
)
)
I thought of running a foreach(array as key => values) on each array and sending those values to another array, and then using if(!in_array), but theres got to be a better way to do it.
It sounds like you want the same keys as there is no correlation with the values in the array. If so, you can run a diff on the keys of each sub-array:
if(call_user_func_array('array_diff_key', $array)) {
// not the same keys
} else {
// same keys
}
call_user_func_array() takes the array as an array of arguments and passes each to array_diff_key()
If the result is not empty then there are differences
If the result is empty then there are no differences
I recommend using an array in this way:
Array
(
[option] => Array
(
[C] => Array
(
[price] => 100
[cost] => 0
)
[M] => Array
(
[price] => 200
[cost] => 0
)
[G] => Array
(
[price] => 300
[cost] => 0
)
)
)
PHP Code:
$product = array("option" => array("C" => array("price" => 100, "cost" => 0), "M" => array("price" => 200, "cost" => 0), "G" => array("price" => 300, "cost" => 0)));
hi i have the follwing array structure
Array
(
[_id] => MongoId Object
(
[$id] => 538978ce8ead0ec1048b456c
)
[cartId] => 98374319ff71dbc3a84b842b7a443cf7
[products] => Array
(
[0] => Array
(
[productId] => 100343
[quantity] => 17
[name] => a
)
[1] => Array
(
[productId] => 100344
[quantity] => 3
[name] => ab
)
[2] => Array
(
[productId] => 100345
[quantity] => 1
[name] => abc
)
)
And i'm having problems to increment the quantity of the products based on the productId
I now use the position but i have no reference on id
$oCartsCollection->update(array('cartId'=>'98374319ff71dbc3a84b842b7a443cf7'), array('$inc' => array('products.0.quantity'=>1)));
What you need to do is add to your query to select and element from your array and then use the positional $ operator in order to match that position:
$oCartsCollection->update(
array(
'cartId'=>'98374319ff71dbc3a84b842b7a443cf7',
'products.productId' => 100343
),
array('$inc' => array('products.$.quantity'=>1)));
The "dot" notation method is fine for accessing the productId element in this case. For multiple fields to match use $elemMatch instead
$oCartsCollection->update(
array(
'cartId'=>'98374319ff71dbc3a84b842b7a443cf7',
'products' => array(
'$elemMatch' => array(
'productId' => 100343,
'name' => 'a'
)
)
),
array('$inc' => array('products.$.quantity'=>1)));
Hello people here is the code that i was using initially....
array_push($Parameter_IdArray, $Parameter_Id1, $Parameter_Id2, $Parameter_Id3, $OptParameter_Id);
array_push($Eqt_ParamArray, $eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
i had no issues to push array values .... but now $eqt_param1, $eqt_param2, $eqt_param3 and $Opt_eqt_param1 are in one more array it is something like this
Array ( [Profile_Id] => 4 [eqt_param] => Array ( [0] => 4.00 [1] => 4.00 [2] => 4.00 ) [Parameter_Id1] => 8 [min_param] => Array ( [0] => 1.00 [1] => 1.00 [2] => 1.00 ) [max_param] => Array ( [0] => 5.00 [1] => 5.00 [2] => 5.00 ) [Wtg_param] => Array ( [0] => 25.00 [1] => 25.00 [2] => 50.00 ) [Parameter_Id2] => 5 [Parameter_Id3] => 1 [Opt_eqt_param] => Array ( [0] => 0.00 ) [OptParameter_Id] => 14 [Opt_wtg] => Array ( [0] => 1.05 ) [operator] => Array ( [0] => M ) [eqt_pay] => 1,574,235 [rec_sal] => 1,485,000 [#] => -6.01 % [Emp_Id] => 490699 [Emp_Process] => Confirm New Pay )
now i need tp push array values $eqt_param1, $eqt_param2, $eqt_param3 and $Opt_eqt_param1 to $Eqt_ParamArray how to do that?
If I understand correctly, you've now got an associative array in PHP. On that assumption, I went ahead and reformatted the nightmare for you:
$myarray = array(
"Profile_Id" => 4,
"eqt_param" => array(4.00, 4.00, 4.00),
"Parameter_Id1" => 8,
"min_param" => array (1.00, 1.00, 1.00 ),
"max_param" => array (5.00, 5.00, 5.00 ),
"Wtg_param" => array (25.00, 25.00, 50.00 ),
"Parameter_Id2" => 5,
"Parameter_Id3" => 1,
"Opt_eqt_param" => array (0.00),
"OptParameter_Id" => 14,
"Opt_wtg" => array (1.05),
"operator" => array ("M"),
"eqt_pay" => array(1,574,235),
"rec_sal" => array(1,485,000),
"#" => "-6.01 %",
"Emp_Id" => 490699,
"Emp_Process" => "Confirm New Pay"
);
What this good formatting now makes clear is that you can use array_push directly on the "eqt_param" index:
array_push($myArray["eqt_param"], $eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
You may also mean that you want to replace it, which is easy too:
$myArray['eqt_param'] = array($eqt_param1, $eqt_param2, $eqt_param3, $Opt_eqt_param1);
The same principles apply in Javascript, which you have had tagged so maybe you mean that.