Merge rows between two arrays of objects based on column value [duplicate] - php

This question already has answers here:
Merging and group two arrays containing objects based on one identifying column value
(4 answers)
Closed 5 months ago.
After I merged two arrays like this array_merge($array1, $array2);, it becomes like this:
array(10) {
[0]=>
object(stdClass) (2) {
["id"]=>
string(1) "1"
["text"]=>
string(5) "one"
}
[1]=>
object(stdClass) (2) {
["id"]=>
string(1) "2"
["text"]=>
string(8) "two"
}
[2]=>
object(stdClass) (2) {
["id"]=>
string(1) "3"
["text"]=>
string(4) "three"
}
[3]=>
object(stdClass) (2) {
["id"]=>
string(1) "4"
["text"]=>
string(8) "four"
}
[4]=>
object(stdClass) (2) {
["id"]=>
string(1) "5"
["text"]=>
string(3) "five"
}
[5]=>
object(stdClass) (2) {
["id"]=>
string(1) "1"
["unit"]=>
string(1) "0"
}
[6]=>
object(stdClass) (2) {
["id"]=>
string(1) "2"
["unit"]=>
int(0)
}
[7]=>
object(stdClass) (2) {
["id"]=>
string(1) "3"
["unit"]=>
int(0)
}
[8]=>
object(stdClass) (2) {
["id"]=>
string(1) "4"
["unit"]=>
string(1) "0"
}
[9]=>
object(stdClass) (2) {
["id"]=>
string(1) "5"
["unit"]=>
int(1)
}
}
Which means both arrays are literally merged. But what I wanted is since both arrays has common property called id and same value for it, it should become like:
array(2) {
[0]=>
object(stdClass) (2) {
["id"]=>
string(1) "1"
["text"]=>
string(5) "one"
["unit"]=>
int(0)
}
[1]=>
object(stdClass) (2) {
["id"]=>
string(1) "2"
["text"]=>
string(8) "two"
["unit"]=>
int(2)
}
}
Note that array1 has id, text while array2 has id and unit.
I did refer here as tried the first answer which suggest to use array_map(), but for me I'm getting error saying argument 1 is not an array.
Combine two arrays into a single array based on a common column value
EDIT:
tried this (doesn't work):
$array1 = array_walk($array1, function(&$value) { $value = (array) $value; })
$array2 = array_walk($array2, function(&$value) { $value = (array) $value; })
function modifyArray($a, $b)
{
if (!empty($a) && !empty($b)) {
return array_merge($a, $b);
} else if (!empty($a) && empty($b)) {
return $a;
} else if (empty($a) && !empty($b)) {
return $b;
}
}
$new = array_map("modifyArray", $array1, $array2);
var_dump($new);

Merging objects is noticeably more tedious than arrays. I'd be tempted to convert the array of objects to an array or arrays in my own project, but I won't for this solution.
Unlike arrays which can enjoy array_merge() or the union operator, objects need to be pushed in manually.
I am temporarily grouping data by using the id values as first level keys in the loop, then optionally sorting by those keys, then re-indexing the output to remove the temporary keys.
Code: (Demo):
$output = [];
foreach ($poorly_merged as $object) {
if (!isset($output[$object->id])) {
$output[$object->id] = $object;
} else {
foreach ($object as $property => $value) {
$output[$object->id]->{$property} = $value;
}
}
}
ksort($output); // optionally order by ids
var_export(array_values($output));
Or:
$output = [];
foreach ($poorly_merged as $object) {
if (!isset($output[$object->id])) {
$output[$object->id] = (object)[];
}
foreach ($object as $property => $value) {
$output[$object->id]->{$property} = $value;
}
}
ksort($output); // optionally order by ids
var_export(array_values($output));
Better practice would be not to merge your twp input arrays to form the $poorly_merged array. You could use iterate the second array of objects and add that data into the first -- this would be a more direct solution.

Related

How to loop through array of multiple arrays in php

I am trying to loop through array of arrays in php. Usually get stalked with complex array sometimes but I need your kind assistance with this.
var_dump($array) produced the array below:
$arrayVal = array(6) {
["item_id"]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
["request_explanation"]=>
array(2) {
[0]=>
string(7) "Welcome"
[1]=>
string(11) "Hello World"
}
["quantity"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
["unit_cost"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["total_cost"]=>
array(2) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
}
["supporting_document"]=>
string(0) ""
}
My database table:
I want to be able to save each of the value in that array into the table above. Thanks for helping me.
Use the indexes of one of the sub-arrays to access all the other sub-arrays:
foreach ($array['item_id'] as $i => $item_id) {
$request_explanation = $array['request_explanation'][$i];
$quantity = $array['quantity'][$i];
// repeat this for all the columns
// Now you can insert all these variables into the database
}
Use a loop to build 2 separate arrays:
foreach($array['ExpensesList'] as $index => $val){
$array1[$index] = $array['ExpensesList'][$index][0];
$array2[$index] = $array['ExpensesList'][$index][1];
}
Then insert each array into your database individually.
This will not work if any sub array contains an index at 2, so this is explicitly for the example structure you provided.

How to merge two arrays with same id? [duplicate]

This question already has answers here:
Merging and group two arrays containing objects based on one identifying column value
(4 answers)
Closed last month.
first arrays has two properties:
id, text
second array has two properties
id, count
How to combine these into one array by id where the new array will have three properties:
id
text
count
First array:
array(2) {
[0]=>
object(stdClass)#351 (2) {
["id"]=>
string(1) "1"
["text"]=>
string(5) "tree"
}
[1]=>
object(stdClass)#348 (2) {
["id"]=>
string(1) "2"
["text"]=>
string(8) "house"
}
second array:
array(2) {
[0]=>
object(stdClass)#351 (2) {
["id"]=>
string(1) "1"
["count"]=>
string(5) "3"
}
[1]=>
object(stdClass)#348 (2) {
["id"]=>
string(1) "2"
["count"]=>
string(8) "4"
}
I tried:
array_merge_recursive, array_merge where these only merge the two arrays together into one long array.
Expected output with above arrays:
array(2) {
[0]=>
object(stdClass)#351 (2) {
["id"]=>
string(1) "1"
["count"]=>
string(5) "3"
string(1) "1"
["text"]=>
string(5) "tree"
}
[1]=>
object(stdClass)#348 (2) {
["id"]=>
string(1) "2"
["count"]=>
string(8) "4"
string(1) "2"
["text"]=>
string(8) "house"
}
You could write a simple function that would merge the properties of the object in the second array if it matches the id of the object in the first array:
function obj_array_merge( $a1, $a2 ) {
$newAry = [];
foreach( $a1 as $idx => $obj1 ) {
// Clone object to prevent alterations to object in $a1
$newAry[$idx] = clone $obj1;
foreach($a2 as $obj2) {
/**
** If id property of both objects match,
** copy properties from second array object
** to new object (clone of first array object).
*/
if( $newAry[$idx]->id === $obj2->id ) {
foreach($obj2 as $prop => $val) {
$newAry[$idx]->$prop = $val;
}
}
}
}
return $newAry;
}
// To run:
$mergedArray = obj_array_merge( $array1, $array2 );
However, note that this is not going to be fast. With a large number of objects it can get quite slow since it has to iterate over both arrays to check for matches. It will also overwrite properties that exist in the new array object with that of the second array object (you didn't specify if that was an issue).

PHP - Multidimensional array epanded from variable

I have the following array stored in $members
array(3) {
[0]=> array(2) {
["index"]=> string(1) "1"
["routePartitionName"]=> string(20) "US-555-foop-GWRoutes" }
[1]=> array(2) {
["index"]=> string(1) "2"
["routePartitionName"]=> string(27) "Cluster DN Presence Allowed" }
[2]=> array(2) {
["index"]=> string(1) "3"
["routePartitionName"]=> string(26) "Cluster DN Presence Denied" }
}
I'm trying to embed this into another array during a foreach loop. However it seems to evaluate the $members variable as text.
$programTags[] = array(
"name"=>"$cssname",
"description"=>"$cssdescription",
"members"=>"$members");
How can I expand the variable thus creating a multidimensional array?
Remove the quotes from $members variable, it convert it into string.
$programTags[] = array(
"name"=>"$cssname",
"description"=>"$cssdescription",
"members"=>$members);
using nested foreach
foreach($members as $array)
{
//$array is array variable .
foreach($array as $values)
{
//your code here
}
}
exapmple:
$member = array(3) {
[0]=> array(2) {
["index"]=> string(1) "1"
["routePartitionName"]=> string(20) "US-555-foop-GWRoutes" }
[1]=> array(2) {
["index"]=> string(1) "2"
["routePartitionName"]=> string(27) "Cluster DN Presence Allowed" }
[2]=> array(2) {
["index"]=> string(1) "3"
["routePartitionName"]=> string(26) "Cluster DN Presence Denied" }
}
applaying foreach then,
$array = array(2) {
["index"]=> string(1) "1"
["routePartitionName"]=> string(20) "US-555-foop-GWRoutes"
}

Fetch value from array/string

How can I fetch the value "3" from this set of arrays:
array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "0" }
The arrays are output from a foreach statement of parenting array, which is:
array(3) { [0]=> string(8) "St" [1]=> string(1) "1" [2]=> string(1) "0" }
array(3) { [0]=> string(16) "Fu" [1]=> string(1) "3" [2]=> string(1) "0" }
array(3) { [0]=> string(13) "Pa" [1]=> string(1) "0" [2]=> string(1) "0" }
Where I am going for the second line value: "Fu" [1]=> string(1) "3"
Maybe I am doing it wrong from the first array?
You're not giving us much to go on. Are the 3 arrays already in a parent array, in an object, etc.? Below is how to get the # 3 from the 3 arrays...but I'm guessing this is not actually what you are asking, we likely need much more detail...the real problem you are trying to solve.
function getThree($arr1, $arr2, $arr3) {
$array = array();
$array[] = $arr1;
$array[] = $arr2;
$array[] = $arr3;
foreach( $array AS $subArray ) {
// whichever condition works for you
if( $subArray[0] == 'Fu' || $subArray[1] == 3 ) {
return $subArray;
}
}
}

how do i convert this array?

i have the following array:
["addToCart"]=>
array(3) {
[1]=>
array(5) {
["aantal"]=>
int(1)
["film_id"]=>
string(1) "1"
["zaal_id"]=>
string(1) "1"
["dag"]=>
string(7) "maandag"
["seats"]=>
array(4) {
[0]=>
string(2) "67"
[1]=>
string(2) "68"
[2]=>
string(2) "69"
[3]=>
string(2) "70"
}
}
You can see that i have an array called "seats" inside the "addToCart" array.There are 4 items in the "seats" array.
what i would like to have is 4 separate arrays, they should all have the same content but each of them needs to have 1 value of "seats".
I'm not sure I got exactly what you're looking to do, but this would result in an array of arrays where each has only one seat:
$seatArrays = array();
foreach ($addToCart as $arr)
{
foreach ($arr["seats"] as $seat)
{
$seatArr = $arr; // Copy the original array
$seatArr["seats"] = $seat; // Replace the "seats" subarray with the current seat
$seatArrays[] = $seatArr;
}
}

Categories