How to combine two arrays in php inside foreach loop? - php

I have product options that came from database and I want to merge all the values by same product_id .
I'm using a foreachloop.
stdClass Object(
[id] => 22
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 81
[option_id] => 22
[name] => Black
[value] => black
)
)
),
stdClass Object(
[id] => 10
[product_id] => 50
[values] => Array (
[0] => stdClass Object (
[id] => 33
[option_id] => 10
[name] => L
[value] => l
),
[1] => stdClass Object (
[id] => 34
[option_id] => 10
[name] => M
[value] => m
)
)
),
stdClass Object(
[id] => 24
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 98
[option_id] => 11
[name] => X
[value] => x
),
[1] => stdClass Object (
[id] => 99
[option_id] => 11
[name] => XL
[value] => xl
),
[2] => stdClass Object (
[id] => 100
[option_id] => 11
[name] => XLL
[value] => xll
)
)
)
I want to combine these array by same product_id and get output like :
stdClass Object(
[id] => 22
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 81
[option_id] => 22
[name] => Black
[value] => black
),
[1] => stdClass Object (
[id] => 98
[option_id] => 11
[name] => X
[value] => x
)
[2] => stdClass Object (
[id] => 99
[option_id] => 11
[name] => XL
[value] => xl
),
[3] => stdClass Object (
[id] => 100
[option_id] => 11
[name] => XLL
[value] => xll
)
),
stdClass Object(
[id] => 10
[product_id] => 50
[values] => Array (
[0] => stdClass Object (
[id] => 33
[option_id] => 10
[name] => L
[value] => l
),
[1] => stdClass Object (
[id] => 34
[option_id] => 10
[name] => M
[value] => m
)
)
)
I'm using foreach loop , How to do this inside foreach loop ?
I want to merge values of same product_id. What is the best way to do this ?
My code is:
foreach($data['products'] as $pro){
foreach($pro->options as $opt){
debug($opt);
}
}

You can try something like this :
1/ Your data
$array = /* array with all your product obj */;
// Create your new result array
$result = array();
2/ The foreach loop
// Loop throught all your object
foreach ($array as $obj) {
// Your current product id
$current_product_id = $obj->product_id;
// The values of this product
$current_product_values = $obj->values;
// Loop through all the value of the product
foreach ($current_product_values as $value) {
// Add those value to your result array for the current product id
$result[$current_product_id][] = $value;
}
}
Is it what you are looking for?

Related

Merging two objects based on key value

I have two objects from an API I am trying to merge, one is a list of available subscription packages and one is a list of the current user's active subscription's and the corresponding status.
The first object looks like this:
This\Collection Object
(
[object] => list
[data] => Array
(
[0] => This\Product Object
(
[id] => 444
[name] => Test Plan 4
)
[1] => This\Product Object
(
[id] => 333
[name] => Test Plan 3
)
[2] => This\Product Object
(
[id] => 222
[name] => Test Plan 2
)
[3] => This\Product Object
(
[id] => 111
[name] => Test Plan 1
)
)
[has_more] =>
)
This second object looks like this:
Array
(
[0] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1670307654
[current_period_start] => 1667715654
[plan] => This\Plan Object
(
[currency] => usd
[product] => 111
)
[start_date] => 1667715654
[status] => active
)
[1] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1770507654
[current_period_start] => 1767715454
[plan] => This\Plan Object
(
[currency] => usd
[product] => 333
)
[start_date] => 1767715654
[status] => active
)
)
I want to combine the second object with the first object if subscription->plan->product matches product->id so it would look something like this:
This\Collection Object
(
[object] => list
[data] => Array
(
[0] => This\Product Object
(
[id] => 444
[name] => Test Plan 4
)
[1] => This\Product Object
(
[id] => 333
[name] => Test Plan 3
[subscription] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1770507654
[current_period_start] => 1767715454
[plan] => This\Plan Object
(
[currency] => usd
[product] => 333
)
[start_date] => 1767715654
[status] => active
)
)
[2] => This\Product Object
(
[id] => 222
[name] => Test Plan 2
)
[3] => This\Product Object
(
[id] => 111
[name] => Test Plan 1
[subscription] => This\Subscription Object
(
[id] => sub_xxx
[current_period_end] => 1670307654
[current_period_start] => 1667715654
[plan] => This\Plan Object
(
[currency] => usd
[product] => 111
)
[start_date] => 1667715654
[status] => active
)
)
)
[has_more] =>
)
I know I can perform two foreach loops on each object and turn them into arrays with the revenant data I need and combine them that way, but I would rather merge them complete as objects.
How can I merge/combine these two in PHP?
Edit: This was my attempt at merging the two, but I am lost on how to actually make it work:
foreach($array1 as $product) {
foreach($array2 as $subscription) {
if ($subscription->plan->product == $product->id){
//how do I append to the object in the right place (where the 0 is)?
$array1->data[0]->subscription = $subscription->plan;
}
}
}

How to get product image from magento database using magento api in Magento 1.9.2.2?

I am trying to fetch complete data of products from magento database using magento soap api using this link: http://devdocs.magento.com/guides/m1x/api/soap/catalog/catalogProduct/catalog_product.list.html
and using the following code:
<?php
$proxy = new SoapClient('http://xxxxxxx.com/api/v2_soap/?wsdl'); // TODO : change url
$sessionId = $proxy->login('test_role', 'password'); // TODO : change login and pwd if necessary
$result = $proxy->catalogProductList($sessionId);
print_r($result);
?>
I do get data of all the products in the inventory like this:
Array ( [0] => stdClass Object ( [product_id] => 24 [sku] => 123445 [name] => Burger [set] => 4 [type] => simple [category_ids] => Array ( [0] => 59 ) [website_ids] => Array ( [0] => 1 ) ) [1] => stdClass Object ( [product_id] => 25 [sku] => MG1456 [name] => Massage [set] => 4 [type] => simple [category_ids] => Array ( [0] => 63 ) [website_ids] => Array ( [0] => 1 ) ) [2] => stdClass Object ( [product_id] => 26 [sku] => 345666 [name] => Chicken Chilly [set] => 4 [type] => simple [category_ids] => Array ( [0] => 59 ) [website_ids] => Array ( [0] => 1 ) ) [3] => stdClass Object ( [product_id] => 27 [sku] => 23424 [name] => Chicken Biryani [set] => 4 [type] => simple [category_ids] => Array ( [0] => 59 ) [website_ids] => Array ( [0] => 1 ) ) [4] => stdClass Object ( [product_id] => 28 [sku] => 45567 [name] => Panner Chilly [set] => 4 [type] => simple [category_ids] => Array ( [0] => 59 ) [website_ids] => Array ( [0] => 1 ) ) [5] => stdClass Object ( [product_id] => 31 [sku] => S5GH488 [name] => Pizza [set] => 4 [type] => simple [category_ids] => Array ( [0] => 59 ) [website_ids] => Array ( [0] => 1 ) ) )
But I ALSO NEED THE IMAGE OF EACH PRODUCT SO I CAN DISPLAY IT IN MY APP! PLEASE HELP!
Now that you have all the products in your $result array could loop through this and get the images.
As you can see in the official Magento docs you retrieve the images like so, adjusted to your current script:
<?php
$proxy = new SoapClient('http://xxxxxxx.com/api/v2_soap/?wsdl'); // TODO : change url
$sessionId = $proxy->login('test_role', 'password'); // TODO : change login and pwd if necessary
$result = $proxy->catalogProductList($sessionId);
$productImages = array();
// Getting all the product images
foreach($result as $product) {
$productImages[] = $proxy->catalogProductAttributeMediaList($sessionId, $product->productId);
}
print_r($result);
// Show the product images array
print_r($productImages);
?>

How to convert DB table with parent son relation to multi-dimensional array

How can I convert a database table like that:
into a multi-dimensional array like that ?
I tried a recursive loop like below but can't get it to display properly.
function cost_centres_format($items)
{
foreach ($items as $item) {
echo $item->name.' - '.$item->parent_id;
echo '<br/>';
$sons = $this->purchase_order_model->get_cost_centre_sons($item->internal_purchase_order_cost_centre_id);
if(count($sons)>0){
$this->cost_centres_format($sons);
}
}
}
Here is my solution for it:
function cost_centres_format($items,$parent_id,$array=array()) {
foreach($items as $item) {
if($item->parent_id == $parent_id) {
$array[] = $item;
if($item->internal_purchase_order_cost_centre_id>0) {
$array = cost_centres_format($items,$item->internal_purchase_order_cost_centre_id,$array);
}
}
}
return $array;
}
$array = cost_centres_format($items,0);
Diesel (id:5) will be below Vehicle Maintenance (id:4) because of its original order. You can do an additional sort by name but in your example Capital (id:3) was below Overheads (id:2).
The above code doesn't produce multidimensional array but instead one dimensional array like so :
Array
(
[0] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 1
[name] => Direct Expenses
[parent_id] => 0
)
[1] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 4
[name] => Vehicle Maintenance
[parent_id] => 1
)
[2] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 9
[name] => CN09 AKO
[parent_id] => 4
)
[3] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 10
[name] => DY52 BYO
[parent_id] => 4
)
[4] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 14
[name] => MX08 MVJ
[parent_id] => 4
)
[5] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 15
[name] => YJ55 TXA
[parent_id] => 4
)
[6] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 5
[name] => Diesel
[parent_id] => 1
)
[7] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 6
[name] => Vehicle Rent
[parent_id] => 1
)
[8] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 11
[name] => Vehicle Repair
[parent_id] => 1
)
[9] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 16
[name] => CN09 AKO
[parent_id] => 11
)
[10] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 17
[name] => DY52 BYO
[parent_id] => 11
)
[11] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 18
[name] => MX08 MVJ
[parent_id] => 11
)
[12] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 19
[name] => YJ55 TXA
[parent_id] => 11
)
[13] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 2
[name] => Overheads
[parent_id] => 0
)
[14] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 12
[name] => Internet Service
[parent_id] => 2
)
[15] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 13
[name] => Warehouse Rent
[parent_id] => 2
)
[16] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 3
[name] => Capital
[parent_id] => 0
)
[17] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 7
[name] => New Stock
[parent_id] => 3
)
[18] => stdClass Object
(
[internal_purchase_order_cost_centre_id] => 8
[name] => New Vehicle
[parent_id] => 3
)
)

Sort JSON object in PHP by a key value [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
I need to sort this kind of information by the "score" value in a PHP script, how can I do? :
Array
(
[0] => stdClass Object
(
[name] => Morts par Déshydratation
[score] => 4
[id] => dwater
)
[1] => stdClass Object
(
[name] => Réparations de chantiers
[score] => 87
[id] => brep
)
[2] => stdClass Object
(
[name] => Campeur téméraire
[score] => 77
[id] => camp
)
[3] => stdClass Object
(
[name] => Décoration
[score] => 112
[id] => deco
)
)
PS : This is already in a PHP value, I already used json_decode.*
Where $data looks like this:
Array
(
[0] => stdClass Object
(
[name] => Morts par Déshydratation
[score] => 4
[id] => dwater
)
[1] => stdClass Object
(
[name] => Réparations de chantiers
[score] => 87
[id] => brep
)
[.] => ....
)
You can use usort() to sort the array :
<?php
usort($data, function($a, $b) { //Sort the array using a user defined function
return $a->score > $b->score ? -1 : 1; //Compare the scores
});
print_r($data);
?>
Outputs:
Array
(
[0] => stdClass Object
(
[name] => Décoration
[score] => 112
[id] => deco
)
[1] => stdClass Object
(
[name] => Réparations de chantiers
[score] => 87
[id] => brep
)
[2] => stdClass Object
(
[name] => Campeur téméraire
[score] => 77
[id] => camp
)
[3] => stdClass Object
(
[name] => Drogues
[score] => 49
[id] => drug
)
[4] => stdClass Object
(
[name] => Ouverture de porte
[score] => 11
[id] => door
)
[5] => stdClass Object
(
[name] => Morts par Déshydratation
[score] => 4
[id] => dwater
)
)

php codeigniter select results from foreach and save into an array

I have a php mysql query results as following...
Array
(
[0] => stdClass Object
(
[id] => 52
[sku] =>
[name] => stone product 52
)
[1] => stdClass Object
(
[id] => 53
[sku] =>
[name] => stone product 53
)
.
.
.
)
Array
(
[0] => stdClass Object
(
[id] => 12
[sku] =>
[name] => stone product 12
)
[1] => stdClass Object
(
[id] => 13
[sku] =>
[name] => stone product 13
)
.
.
.
)
And I want the results just like as below
Array(
[0] => stdClass Object
(
[id] => 52
[sku] =>
[name] => stone product 52
)
[1] => stdClass Object
(
[id] => 53
[sku] =>
[name] => stone product 53
)
[2] => stdClass Object
(
[id] => 12
[sku] =>
[name] => stone product 12
)
[3] => stdClass Object
(
[id] => 13
[sku] =>
[name] => stone product 13
)
in a single array
Please suggest me php code
You should not be joining these results in php. Use one query to get both sets of results, this will cut down on your db reads.
You can use array_merge function : http://php.net/manual/en/function.array-merge.php
if you are working on an array that has arrays inside you can merge them with a foreach call
$my_array=get_the_result_from_mysql();//Here how do you get the result.
$new_array=array();
foreach ($my_array as $row) {
$new_array=array_merge($new_array,$row);
}
var_dump($new_array);
I think this helps you.

Categories