Update a multidimensional array - php

I'm trying to update my array but it doesn't work as expected!
I got this array:
Array
(
[0] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
)
[1] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
)
[2] => Array
(
[0] => property_unit Object
(
[unit] => one bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 3
[size] => 54
[percentage] => 0
[search] => 1
)
)
)
[1] => property_unit Object
(
[unit] => three bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 90
[percentage] => 0
[search] => 1
)
)
)
[2] => property_unit Object
(
[unit] => two bedroom
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 1
[size] => 74
[percentage] => 0
[search] => 1
)
)
)
[3] => property_unit Object
(
[unit] => studio
[details] => Array
(
[0] => propertydetails Object
(
[priority] => 2
[size] => 22.35
[percentage] => 0
[search] => 1
)
)
)
)
)
and using var_dump()
array (size=24)
0 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
1 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
2 =>
array (size=4)
0 =>
object(property_unit)[1]
public 'unit' => string 'one bedroom' (length=11)
public 'details' =>
array (size=1)
...
1 =>
object(property_unit)[5]
public 'unit' => string 'three bedroom' (length=13)
public 'details' =>
array (size=1)
...
2 =>
object(property_unit)[3]
public 'unit' => string 'two bedroom' (length=11)
public 'details' =>
array (size=1)
...
3 =>
object(property_unit)[7]
public 'unit' => string 'studio' (length=6)
public 'details' =>
array (size=1)
...
3 =>
I got this array with this:
$array = [new property_unit('one bedroom', [new propertydetails(1, 54, 0, 1)]),
new property_unit('two bedroom', [new propertydetails(1, 74, 0, 1)]),
new property_unit('three bedroom', [new propertydetails(1, 90, 0, 1)]),
new property_unit('studio', [new propertydetails(2, 22.35, 0, 1)])];
Then I perform a computation on it to get all the possible array.
function computePermutations($array) {
$result = [];
$recurse = function($array, $start_i = 0) use (&$result, &$recurse) {
if ($start_i === count($array)-1) {
array_push($result, $array);
}
for ($i = $start_i; $i < count($array); $i++) {
//Swap array value at $i and $start_i
$t = $array[$i];
$array[$i] = $array[$start_i];
$array[$start_i] = $t;
//Recurse
$recurse($array, $start_i + 1);
//Restore old order
$t = $array[$i];
$array[$i] = $array[$start_i];
$array[$start_i] = $t;
}
};
$recurse($array);
return $result;
}
//I assign to my array
$array = computePermutations($array);
I thing something maybe goes wrong here ?
but when I'm trying to update my array like :
$array[0][0]->details[0]->priority = 3;
It update all the first priority field, (see the array before) this is after the update.
Meaning:
$array[0][0]->details[0]->priority
$array[1][0]->details[0]->priority
$array[2][0]->details[0]->priority
etc... becomes 3
Any idea where this can come from, is there any other way to update a multidimensional array? Am I doing something wrong here?

Related

Multidimensional array value if exits

I want to bring results that have a status 1 in the array.(Sorry i don't speak english)
My array is;
[10100002] => Array
(
[0] => stdClass Object
(
[ID] => 664
[barcode] => 10100002
[status] => 0
)
[1] => stdClass Object
(
[ID] => 1339
[barcode] => 10100002
[status] => 0
)
)
[10100004] => Array
(
[0] => stdClass Object
(
[ID] => 1116
[barcode] => 10100004
[status] => 1
)
[1] => stdClass Object
(
[ID] => 1826
[barcode] => 10100004
[status] => 0
)
)
in 10100002 two status is 0 but second array found status 1. if the status value is 1 in multiple arrays i like result this ;
[10100004] => Array
(
[0] => stdClass Object
(
[ID] => 1116
[barcode] => 10100004
[status] => 1
)
[1] => stdClass Object
(
[ID] => 1826
[barcode] => 10100004
[status] => 0
)
)
My code is here;
$result = array();
foreach ($fetch_data as $value) {
if($value->status== 1)
$result[$value->barcode][] = $value;
}
// this give me only one result. Output;
[10100004] => Array
(
[0] => stdClass Object
(
[ID] => 1116
[barcode] => 10100004
[status] => 1
)
)
You can use array_walk
$arr = Array(
'10100002' => Array
(
'0' => Array
(
'ID' => 664,
'barcode' => 10100002,
'status' => 0
),
'1' => Array
(
'ID' => 1339,
'barcode' => 10100002,
'status' => 0
)
),
'10100004' => Array
(
'0' => Array
(
'ID' => 1116,
'barcode' => 10100004,
'status' => 1
),
'1' => Array
(
'ID' => 1826,
'barcode' => 10100004,
'status' => 0
)
)
);
$res = [];
array_walk($arr, function($v, $k) use (&$res){
foreach($v as $key => $value){
if($value['status']){
!empty($value['status']) ? ($res[$k][] = (object)$v[$key]) : '';
}
}
});
echo '<pre>';
print_r($res);
Output
Array
(
[10100004] => Array
(
[0] => stdClass Object
(
[ID] => 1116
[barcode] => 10100004
[status] => 1
)
)
)
LIVE DEMO

Sort a multidimensional an array with numeric keys but keep the keys same just change the order [duplicate]

This question already has an answer here:
PHP sort associative array by numeric key in asc order [duplicate]
(1 answer)
Closed 10 months ago.
So I have 3 dimensional array. I want that array to be reordered based on the keys but the value of the keys should remain as it is. Like for an example if the array keys are 5,2,4,1,3 then it should become 1,2,3,4,5. Below I'm providing the array I have and excepted array and the solutions I have tried.
This is the array I have :-
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
Following are the solutions I have tried :-
$result = ksort($result);
$result = array_values($result);
$result = array_splice($result, 0, 0);
$result = sort($result);
$result = array_splice($result, 0, count($result));
This is the expected array :-
Array
(
[1] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => D4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 557.14285714286
[is_freezed] => 1
[can_sell] =>
)
)
)
[2] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E4
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[3] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E5
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 516.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[4] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => C8
[deal_text] =>
[units] => 1
[total_units] => 0
[amount] => 526.66666666667
[is_freezed] => 1
[can_sell] =>
)
)
)
[5] => Array
(
[Anfield] => Array
(
[0] => Array
(
[slot] => E3
[deal_text] =>
[units] => 5
[total_units] => 5
[amount] => 2620.8333333333
[is_freezed] =>
[can_sell] => 1
)
)
)
)
Nothing is working any help will be appreciated. thanks in advance.
You are using ksort as $result = ksort($result);, ksort return TRUE/FALSE. That means you are assigning that to $results.
Read here PHP ksort
Your code should be:-
ksort($results);
instead of
$result = ksort($result);
You can use ksort for the keys sorting, here is an example
$arr = [
5 => [1,3],
3 => [2,3],
2 => [0,7]
];
ksort($arr);
echo '<pre>';
print_r($arr);
Output
Array
(
[2] => Array
(
[0] => 0
[1] => 7
)
[3] => Array
(
[0] => 2
[1] => 3
)
[5] => Array
(
[0] => 1
[1] => 3
)
)

PHP sum array values with same keys

This is the original main array:
Array
(
[0] => Array
(
[subtotal] => 0.6000
[taxes] => 0.0720
[charged_amount] => 0.6720
[total_discount] => 0.0000
[provinceName] => BC
[store_key] => 1
[store_id] => 5834
[categories] => Array
(
[2] => 0.6000
[4] => 0
[3] => 0
)
)
[1] => Array
(
[subtotal] => 29.8500
[taxes] => 2.3270
[charged_amount] => 20.2370
[total_discount] => 11.9400
[provinceName] => MB
[store_key] => 9
[store_id] => 1022
[categories] => Array
(
[2] => 0
[4] => 29.8500
[3] => 0
)
)
[2] => Array
(
[subtotal] => 0.3000
[taxes] => 0.0390
[charged_amount] => 0.3390
[total_discount] => 0.0000
[provinceName] => NB
[store_key] => 8
[store_id] => 1013
[categories] => Array
(
[2] => 0.3000
[4] => 0
[3] => 0
)
)
[3] => Array
(
[subtotal] => 24.3100
[taxes] => 1.1830
[charged_amount] => 10.2830
[total_discount] => 15.2100
[provinceName] => NL
[store_key] => 4
[store_id] => 3033
[categories] => Array
(
[2] => 24.3100
[4] => 0
[3] => 0
)
)
[4] => Array
(
[subtotal] => 1116.3400
[taxes] => 127.6960
[charged_amount] => 1110.0060
[total_discount] => 134.0300
[provinceName] => ON
[store_key] => 2
[store_id] => 1139
[categories] => Array
(
[2] => 85.7300
[4] => 143.2800
[3] => 887.3300
)
)
[5] => Array
(
[subtotal] => 10.8500
[taxes] => 1.4100
[charged_amount] => 12.2600
[total_discount] => 0.0000
[provinceName] => ON
[store_key] => 5
[store_id] => 1116
[categories] => Array
(
[2] => 10.8500
[4] => 0
[3] => 0
)
)
)
I just need to add the values of the array [categories] with same keys and use it further to print the total, but not getting correct output, can someone help me out to get the desired result:
Desired result
An array with same keys but total of individual array values
Array ( [2] => 0.9000 [4] => 29.8500 [3] => 1.5 )
NOTE: Initial array is dynamic can have n number of key value pair
Thanks
The first thing that you need to do is iterate through the outer array. Then, for each row in the outer array, you and to iterate through each entry in the category element. So this means that we have two foreach loops. Inside the inner foreach, we simply set the value for the current index to be the value of the same index on a 'sum' array (if it doesn't already exist), or increment the value of that index if it already exists in the 'sum' array.
<?php
$sumArray = array();
foreach($outerArray as $row)
{
foreach($row["categories"] as $index => $value)
{
$sumArray[$index] = (isset($sumArray[$index]) ? $sumArray[$index] + $value : $value);
}
}
?>
Demo using your example array

PHP - UNSET Elements in a Deep Mixed Index/Associative Array

I have a mixed index/associative array in PHP that can, in theory, be infinitely deep because some elements may have 'children' arrays. I attach a slimmed down example. The php array is derived from some JSON data.
I want to search on a given key/value pair and to delete any elements matching that pair at the index level.
So for example, in the array below, I wish to search for the key of 'formId' and a value of '44' and to delete elements such that $array[0][0][0][0] and $array[0][1] are removed. Obviously I will then need to re-number the array.
I need to be able to search on any key/value pair... only one pair at a time is necessary... that is to say that the above example of 'formId' of '44' might be 'viewId' of '16' next time.
I have tried numerous ways to do this, with iterators, recursive foreach loops, &$reference keys but I just cannot seem to get it right.
Array
(
[0] => Array
(
[formId] => 0
[subId] => 0
[viewId] => 0
[id] => 0
[children] => Array
(
[0] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 16
[id] => _st_node_5328_0_0
[children] => Array
(
[0] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 16
[id] => _st_node_3838_0_0_0
[children] => Array
(
[0] => Array
(
[formId] => 44
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_0
)
[1] => Array
(
[formId] => 7
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_1
)
[2] => Array
(
[formId] => 3
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_2
)
[3] => Array
(
[formId] => 10
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_3
)
[4] => Array
(
[formId] => 9
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_4
)
[5] => Array
(
[formId] => 8
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_5
)
[6] => Array
(
[formId] => 6
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_6
)
[7] => Array
(
[formId] => 5
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_7
)
[8] => Array
(
[relId] => 167
[formId] => 4
[text] => Laptop Computers
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_8
[isActive] =>
)
[9] => Array
(
[formId] => 1
[subId] =>
[viewId] => 16
[id] => _st_node_947_0_0_0_9
)
)
)
[1] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 1
[id] => _st_node_3838_0_0_1
)
[2] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 11
[id] => _st_node_3838_0_0_2
)
[3] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 12
[id] => _st_node_3838_0_0_3
)
[4] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 13
[id] => _st_node_3838_0_0_4
)
[5] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 14
[id] => _st_node_3838_0_0_5
)
[6] => Array
(
[formId] => 1
[subId] => 0
[viewId] => 110
[id] => _st_node_3838_0_0_6
)
)
)
[1] => Array
(
[formId] => 44
[subId] => 0
[viewId] => 144
[id] => _st_node_5328_0_1
)
[2] => Array
(
[formId] => 10
[subId] => 0
[viewId] => 26
[id] => _st_node_5328_0_2
)
[3] => Array
(
[formId] => 9
[subId] => 0
[viewId] => 9
[id] => _st_node_5328_0_3
)
[4] => Array
(
[formId] => 8
[subId] => 0
[viewId] => 8
[id] => _st_node_5328_0_4
)
[5] => Array
(
[formId] => 7
[subId] => 0
[viewId] => 25
[id] => _st_node_5328_0_5
)
[6] => Array
(
[formId] => 6
[subId] => 0
[viewId] => 6
[id] => _st_node_5328_0_6
)
[7] => Array
(
[formId] => 5
[subId] => 0
[viewId] => 5
[id] => _st_node_5328_0_7
)
[8] => Array
(
[formId] => 4
[subId] => 0
[viewId] => 4
[id] => _st_node_5328_0_8
)
[9] => Array
(
[formId] => 3
[subId] => 0
[viewId] => 3
[id] => _st_node_5328_0_9
)
)
)
)
The idea
Since you want to unset elements, you definitely want to pass the $array into the function as a reference (&$array).
You want to use recursion to allow you to dig as deep as necessary based on the actual array.
You loop over all elements of the array and the first thing you do is check whether an element is an array. If it is, you check if that subarray matches your criteria. If it does, you remove it. If it doesn't you invoke the function with the same $key, $value pair on that subarray.
The code
function removeMatching($key, $value, &$arr) {
// Iterate over all $arr elements
foreach ($arr as $i => $subarr) {
// Skip all elements that are not arrays
if (is_array($subarr)) {
// Check if the sub-array matches the criteria
if (isset($subarr[$key]) && $subarr[$key] === $value) {
// Remove it from the main array if it does
unset($arr[$i]);
} else {
// If the sub-array didn't match the criteria
// Check if it contains any other matching sub-arrays
removeMatching($key, $value, $arr[$i]);
}
}
}
}
Apply like this:
$array = []; // your array
removeMatching('formId', 44, $array);
var_dump($array);

Convert WSDL to array

I have a WSDL that is mapped to a variable
$client = new SoapClient($wsdl, array("connection_timeout"=>10));
It returns an array like:
stdClass Object
(
[getProductsReturn] => Array
(
[0] => stdClass Object
(
[barCode] => 5060285475448
[brandId] => 0
[childProductIds] => stdClass Object
(
)
[childProductIdsAsIntArray] => stdClass Object
(
)
[childProductIdsList] =>
[fullProductPaths] => Candles->Amber And Lavender Scented Sachet
[fullProductPathsArray] => stdClass Object
(
)
[id] => 5883
[image] =>
[imageId] => 0
[itemCount] => 1
[longDescription] =>
[masterProductId] => 5883
[message] =>
[messagePrice] => 0
[messageType] => 0
[optionId] => 0
[optionName] => null
[optionSetId] => 0
[optionSetName] => null
[orderType] => 5050
[parentProductCode] =>
[parentProductId] => 8088
[price] => 2.99
[productCode] => AA2485
[productId] => 0
[productType] => 1010
[quantity] => 0
[quantityPrice] => 0
[rrp] => 2.99
[shortDescription] => Amber And Lavender Scented Sachet
[variation] =>
[vatCodeDomainId] => 0
[wrappingPrice] => 0
[wrappingType] => 0
)
[1] => stdClass Object
(
[barCode] => 5060285475547
[brandId] => 0
[childProductIds] => stdClass Object
(
)
[childProductIdsAsIntArray] => stdClass Object
(
)
[childProductIdsList] =>
[fullProductPaths] => Candles->Lavender And Bergamot Scented Sachet
[fullProductPathsArray] => stdClass Object
(
)
[id] => 5881
[image] =>
[imageId] => 0
[itemCount] => 1
[longDescription] =>
[masterProductId] => 5881
[message] =>
[messagePrice] => 0
[messageType] => 0
[optionId] => 0
[optionName] => null
[optionSetId] => 0
[optionSetName] => null
[orderType] => 5050
[parentProductCode] =>
[parentProductId] => 8088
[price] => 2.99
[productCode] => AA2484
[productId] => 0
[productType] => 1010
[quantity] => 0
[quantityPrice] => 0
[rrp] => 2.99
[shortDescription] => Lavender And Bergamot Scented Sachet
[variation] =>
[vatCodeDomainId] => 0
[wrappingPrice] => 0
[wrappingType] => 0
)
I want to show this in a HTML and this is what I had given in the for loop:
<pre>
foreach ($product as $products) {
$currentStockQuantity = $product->barCode($products);
echo $currentStockQuantity;
echo $products->barCode;
echo $products[$i]->shortDescription;
echo $products[$i]->barCode;
$i++;
}
But it is not showing all the products, there are about 100 products but only one is showing at the moment
Assuming you stored the result array from your WSDL in a $products variable, you could try:
foreach ($products as $product) {
echo $product->shortDescription;
echo $product->barCode;
// ... and whatever you want
}
More information about foreach: http://php.net/manual/en/control-structures.foreach.php

Categories