Why this array prints only last value outside of the loop? PHP - php

I am having this strange problem. I have a loop where I am looping through an array and executing a query for each id I get in the loop:
$select_deleted_arr=[];
foreach ($selected_devices as $devs) {
$select_deleted_devices->bind_param("i", $devs);
$select_deleted_devices->execute();
$select_deleted_devices_res = $select_deleted_devices->get_result();
$select_deleted_arr = $select_deleted_devices_res->fetch_all(MYSQLI_ASSOC);
}
$select_deleted_devices->close();
print_r($select_deleted_arr);
which prints :
Array ( [0] => Array ( [serial_imei] => 573 [serial_no] => 88888893 [name] => test device [device_no] => 1233 [barcode] => 44556633 ) )
return;
And when I print the array inside the loop:
Array ( [0] => Array ( [serial_imei] => 569 [serial_no] => 88888889 [name] => test device [device_no] => 1233 [barcode] => 44556633 ) ) Array ( [0] => Array ( [serial_imei] => 573 [serial_no] => 88888893 [name] => test device [device_no] => 1233 [barcode] => 44556633 ) )
I have no idea why is it behaving like this because I am doing exactly the same thing multiple times in my project but it always works perfectly fine. Any idea?

Related

PHP Foreach query checking

Array
(
[0] => Array
(
[cart_id] => 24763
[product_id] => 740
[name] => Samsung S20
[model] => Samsung S20
[shipping] => 1
[image] => catalog/samsung/samsungs20.jpg
[option] => Array
(
)
[download] => Array
(
)
)
[1] => Array
(
[cart_id] => 24799
[product_id] => 749
[name] => Huawei P40
[model] => Huawei P40
[shipping] => 1
[image] => catalog/huawei/huaweip40.jpg
[option] => Array
(
)
[download] => Array
(
)
)
)
^ Above is the output of the array
$productdata = $this->cart->getProducts();
^ Above is the code for query out array
is there anyway to check product_id of each array ? Let say that if
product_id = 749 , alert ('P40');
Tried to do
foreach ($productdata as $productdatas) {
if ($productdata['product_id'] = 749)
{ alert('P40');
}
}
nothing happen
This part sets the key, not read it. You need to replace the = with ==:
// From
if ($productdata['product_id'] = 749)
// To
if ($productdata['product_id'] == 749)

search and unset array in array by value in php

My array look like this :
[cart_seller] => Array
(
[3] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
)
)
[4] => Array
(
[# 801] => Array
(
)
)
)
[cart_product] => Array
(
[หมวด1เลือก1 หมวด2เลือก1 1705] => Array
(
[id] => 1705
[name] => ทดสอบสินค้า
[image] => p1534937865-VASAVAT LAB N MEDIA LOGO W.png
[price] => 1111
[option] => หมวด1เลือก1 หมวด2เลือก1
[amount] => 1
)
[# 801] => Array
(
[id] => 801
[name] => โบว์แพรแถบ ร.9 ชนมพรรษา 84 พรรษา ปีพุทธศักราช 2554
[image] => p1498062217-ส.jpg
[price] => 90
[option] =>
[amount] => 1
)
)
I want unset '# 801' in cart_seller and cart_product
in cart_product use unset($cart['cart_product'][# 801]);
but in cart_seller it in array [4] what can i do without reference value (4) ?
exapmle unset($cart['cart_seller'][xxxx][# 801]);
Just loop cart seller array til you find #801.
foreach($cart['cart_seller'] as $key => $c){
if(array_key_exists("#801",$c)){
unset($cart['cart_seller'][$key]['#801']);
}
}

PHP: Displaying data from Bigcommerce API

I am trying to display the data from Bigcommerce through php when I run the Bigcommerce::getCategories() i getting a array of data like this:
[0] => Bigcommerce\Api\Resources\Category Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[fields:protected] => stdClass Object
(
[id] => 88
[parent_id] => 0
[name] => Dell
[description] =>
[sort_order] => 0
[page_title] =>
[meta_keywords] =>
[meta_description] =>
[layout_file] =>
[parent_category_list] => Array
(
[0] => 88
)
[image_file] =>
[is_visible] => 1
[search_keywords] =>
[url] => /dell/
)
[id:protected] => 88
[ignoreIfZero:protected] => Array
(
)
[fieldMap:protected] => Array
(
)
)
but when I try to pass this to JQuery so that I can display it using this statement: <?php echo json_encode($categories); ?> I am getting an array of empty objects, what is the right way to get the array of objects in Bigcommerce API? Thanks.
From the first line of your code:
[0] => Bigcommerce\Api\Resources\Category Object
You have an object, not an array. Try casting it to an array first:
$array = (array) $yourObject;

php- why traversing multidimensional array inside the foreach() doesnt work

I"m trying to traverse on an object i"m getting from openweather.com
without the foreach() i"m getting the right result. inside the foreach() i get an error.
the php code:
$contents = file_get_contents($url);
$clima=json_decode($contents,TRUE);
echo $clima['list'][5]['main']['temp']; // this one works
$i=0;
foreach($clima['list'] as $clima1) {
echo $clima1[$i]['dt']; // here i get PHP Notice: Undefined offset
echo $clima1[$i]['main']['temp']; //here i get PHP Notice: Undefined index: list
$i=$i+1;
}
the error that i get is:
PHP Notice: Undefined offset: 0 // the error is from 0 to 39 and there are 39 objects...
PHP Notice: Undefined index: list
the object beginning and ending part for sample:
Array ( [cod] => 200 [message] => 0.014 [cnt] => 40 [list] =>
Array ( [0] => Array ( [dt] => 1492732800 [main] => Array ( [temp] => 17.64 [temp_min] => 17.04 [temp_max] => 17.64 [pressure] => 1026.03 [sea_level] => 1031.21 [grnd_level] => 1026.03 [humidity] => 100 [temp_kf] => 0.6 ) [weather] => Array ( [0] => Array ( [id] => 800 [main] => Clear [description] => clear sky [icon] => 01n ) ) [clouds] => Array ( [all] => 0 ) [wind] => Array ( [speed] => 1.66 [deg] => 81.5008 ) [sys] => Array ( [pod] => n ) [dt_txt] => 2017-04-21 00:00:00 )
[38] => Array ( [dt] => 1493143200 [main] => Array ( [temp] => 19.72 [temp_min] => 19.72 [temp_max] => 19.72 [pressure] => 1026.92 [sea_level] => 1032.02 [grnd_level] => 1026.92 [humidity] => 87 [temp_kf] => 0 ) [weather] => Array ( [0] => Array ( [id] => 800 [main] => Clear [description] => clear sky [icon] => 01n ) ) [clouds] => Array ( [all] => 0 ) [wind] => Array ( [speed] => 6.95 [deg] => 10.5008 ) [sys] => Array ( [pod] => n ) [dt_txt] => 2017-04-25 18:00:00 )
[39] => Array ( [dt] => 1493154000 [main] => Array ( [temp] => 18.43 [temp_min] => 18.43 [temp_max] => 18.43 [pressure] => 1026.75 [sea_level] => 1031.91 [grnd_level] => 1026.75 [humidity] => 98 [temp_kf] => 0 ) [weather] => Array ( [0] => Array ( [id] => 800 [main] => Clear [description] => clear sky [icon] => 01n ) ) [clouds] => Array ( [all] => 0 ) [wind] => Array ( [speed] => 6.03 [deg] => 9.50076 ) [sys] => Array ( [pod] => n ) [dt_txt] => 2017-04-25 21:00:00 ) )
[city] => Array ( [id] => **** [name] => ***** [coord] => Array ( [lat] => **.**** [lon] => **.**** ) [country] => ** ) )
any help to understand my mistake will be appreciated
It seems, you loop "double". The counter inside the loop is the part, which you don't need, as you already loop over the array with foreach.
I think, the following code would be the better approach
$contents = file_get_contents($url);
$clima=json_decode($contents,TRUE);
echo $clima['list'][5]['main']['temp']; // this one works
foreach($clima['list'] as $clima1) {
echo $clima1['dt']; // here i get PHP Notice: Undefined offset
echo $clima1['main']['temp']; //here i get PHP Notice: Undefined index: list
}
That is because when you foreach over $clima['list'] you will get every values inside $clima['list']. Therefore, first, $clima1 = $clima['list'][0]. After that, $clima1 = $clima['list'][1]... Thus, $clima1 has neither 0 as an index nor 1 nor 2...
What you might do to see it more clearly is this :
foreach($clima['list'] as $key => $clima1)
And everytime, the $key will be your $id. Therefore, you can get rid of your $id and just do it like this :
foreach($clima['list'] as $id => $clima1)
When you run foreach($clima['list'] as $clima1) { ... each object in the loop ($clima1) is equal to $clima['list'][$i] so you don't need to manually put the $i in there.
If your really stuck I'd just run the loop like:
foreach($clima['list'] as $clima1) {
var_dump('<pre>' . $clima1 . '</pre>');
}
To see what the $clima1 variable really is.

How to loop through subarray to see if value is return in string

Here is an example of an array that is output:
Array ( [CART] => Array ( [ITEMS] => Array ( [0] => Array ( [product_id] => 269194 [variation_id] => 0 [options] => Array ( ) [quantity] => 1 [product_name] => 15 Top Hits for Easy Piano [product_code] => HL102668 [product_price] => 14.9900 [original_price] => 14.9900 [default_currency] => 1 [customer_group] => [product_fields] => Array ( ) ) [1] => Array ( [product_id] => 266421 [variation_id] => 0 [options] => Array ( ) [quantity] => 1 [product_name] => Whistle [product_code] => HD245839 [product_price] => 3.9900 [original_price] => 3.9900 [default_currency] => 1 [customer_group] => [product_fields] => Array ( ) ) ) [LAST_UPDATED] => 1349829499 [NUM_ITEMS] => 2 ) [JustAddedProduct] => [CHECKOUT] => Array ( ) )
There is an array for each unique product (in this example there are 2 unique products.) Sometimes there will be just one, sometimes there could be 20 or more unique products.
The value that is important to me is [product_code]. You can see that in the first array, there is [product_code] => HL102668. In the second there is [product_code] => HD245839.
How can I check to see if 'HD' exists in any of the [product_code] values? If it does, I need to return false.
Thank you for your help!
Access your sub array :
$products = $array['CART']['ITEMS'];
Loop through your sub array :
foreach ($products as $product)
Check if HD exists in your product_code, with either simple strstr, or with regex using preg_match (if you are comfortable with it).
if (strstr($product['product_code'], "HD")) {
// Do your actions
}

Categories