php codeigniter select results from foreach and save into an array - php

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.

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;
}
}
}

Issue with foreach Laravel

This Question is continuation of My first question
So I have an array
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[1] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[2] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
)
)
I sort this array
$sorted = $collection->sortByDesc('status');
my view
return view('voyager::users.viewusersAppraisals')->with('values', $sorted);
Now, I got array like
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[2] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[1] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[0] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
)
)
and my foreach loop
#foreach($values as $data)<?php
?>
<tr>
<td>{{$data->name}}</td>
</tr>
#endforeach
I expect output like so
linto
shanu
shelin
joseph
But I get output like so
joseph
linto
shanu
shelin
Any help would be appreciated. Thanks in advance.
It must be your variable be getting overwritten somewhere in the code which you have not mentioned.
Also please dd($sorted) your result after executing the eloquent query to see whether you are getting data from db in right format as per your need.

How to combine two arrays in php inside foreach loop?

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?

merge two multidimensional array based on same ID

I am facing one issue when merging two multidimensional arrays based on the same ID.
In the example below I created two arrays - Array1 and Array2. Both arrays contain objects that have an ID property. Based on the ID property, the arrays should be merged and get the result array:
Array1
Array
(
[0] => stdClass Object
(
[claimtotal] =>
[total] => 4
[ID] => 3
)
[1] => stdClass Object
(
[claimtotal] => 20
[total] => 1
[ID] => 4
)
)
Array2
Array
(
[0] => stdClass Object
(
[ID] =>2
[name] => test1
)
[1] => stdClass Object
(
[ID] => 3
[name] => test2
)
[2] => stdClass Object
(
[ID] =>4
[name] => test3
)
[3] => stdClass Object
(
[ID] => 5
[name] => test4
)
)
Result_array
Array
(
[0] => stdClass Object
(
[ID] =>2
[name] => test1
[claimtotal] =>
[total] =>
)
[1] => stdClass Object
(
[ID] => 3
[name] => test2
[claimtotal] =>
[total] => 4
)
[2] => stdClass Object
(
[ID] =>4
[name] => test3
[claimtotal] => 20
[total] => 1
)
[3] => stdClass Object
(
[ID] => 5
[name] => test4
[claimtotal] =>
[total] =>
)
)
How can I achieve this?
if these are simple objects without methods go:
foreach($firstArray as $key => $firstObject){
foreach($secondArray as $secondObject){
if($firstObject['id'] === $secondObject['id']){
$firstArray[$key] = (object) array_merge((array) $firstObject, (array) $secondObject);
}
}
}
looks messy but does the job without introducing another loop to go through object properties.

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);
?>

Categories