array_column not working for an array of objects? - php

This is my current array structure:
array(2) {
["Ground"] => array(4) {
[0] => object(Shipping\Model\Shipping)#337 (5) {
["shipping_id"] => NULL
["min_weight"] => string(4) "0.00"
["max_weight"] => string(4) "5.00"
["shipping_method"] => string(6) "Ground"
["shipping_rate"] => string(4) "8.00"
}
[1] => object(Shipping\Model\Shipping)#385 (5) {
["shipping_id"] => NULL
["min_weight"] => string(4) "6.00"
["max_weight"] => string(5) "10.00"
["shipping_method"] => string(6) "Ground"
["shipping_rate"] => string(5) "12.00"
}
}
["Expedited"] => array(4) {
[0] => object(Shipping\Model\Shipping)#388 (5) {
["shipping_id"] => NULL
["min_weight"] => string(4) "0.00"
["max_weight"] => string(4) "5.00"
["shipping_method"] => string(9) "Expedited"
["shipping_rate"] => string(5) "12.00"
}
}
}
Every time I run array_column for max_weight I get an empty array as a result. I'm supposed to use the returned array for max().
Is there a workaround for this?

v7.0.0: Added the ability for the input parameter to be an array of objects.
Source: https://secure.php.net/manual/en/function.array-column.php

The array_column is working correctly for me in PHP 7. Here is an example ...
<?php
$shippingMethods = array("Ground" => array());
$gMethod = new stdClass();
$gMethod->shipping_id = NULL;
$gMethod->min_weight = "0.00";
$gMethod->max_weight = "5.00";
$gMethod->shipping_method = "Ground";
$gMethod->shipping_rate = "8.00";
$shippingMethods["Ground"][] = $gMethod;
$gMethod = new stdClass();
$gMethod->shipping_id = NULL;
$gMethod->min_weight = "6.00";
$gMethod->max_weight = "10.00";
$gMethod->shipping_method = "Ground";
$gMethod->shipping_rate = "12.00";
$shippingMethods["Ground"][] = $gMethod;
$shippingMethod = "Ground";
$result = max(array_column(($shippingMethods[$shippingMethod]), "max_weight"));
echo $result;
?>

Related

PHP Array, if duplicate id concat values and delete duplicate

I have an array like this
array(2) {
["sys_ID"]=> string(32) "ab0ce921dba8a810f6db3892399619d9" ["sites"]=> array(5) {
[0]=> array(2) {
["sys_ID"]=> string(32) "448ce5a1dba8a810f6db3892399619ba" ["service"]=> string(4) "IDMB" } [1]=> array(2) {
["sys_ID"]=> string(32) "448ce5a1dba8a810f6db3892399619ba" ["service"]=> string(4) "ODMB" } [2]=> array(2) {
["sys_ID"]=> string(32) "598ce5a1dba8a810f6db3892399619bc" ["service"]=> string(4) "IDMB" } [3]=> array(2) {
["sys_ID"]=> string(32) "876ce5a1dba8a810f6db38923996199f" ["service"]=> string(4) "IDMB" } [4]=> array(2) {
["sys_ID"]=> string(32) "876ce5a1dba8a810f6db38923996199f" ["service"]=> string(4) "ODMB" } } }
If there is a duplicate ['sys_ID'] I want to change the first ['service'] => "IDMB,ODMB" then delete the duplicate value so there is only 1. So the above would become
array(2) {
["sys_ID"]=> string(32) "ab0ce921dba8a810f6db3892399619d9" ["sites"]=> array(5) {
[0]=> array(2) {
["sys_ID"]=> string(32) "448ce5a1dba8a810f6db3892399619ba" ["service"]=> string(4) "IDMB,ODMB" } [1]=> array(2) {
["sys_ID"]=> string(32) "598ce5a1dba8a810f6db3892399619bc" ["service"]=> string(4) "IDMB" } [2]=> array(2) {
["sys_ID"]=> string(32) "876ce5a1dba8a810f6db38923996199f" ["service"]=> string(4) "IDMB,ODMB" } ]} }
The first array was made getting POST values;
<?php
foreach ($_POST['services'] as $item) {
$parts = explode(',', $item);
$siteID = $parts[1];
$services = $parts[0];
$data['sites'][] = [
'sys_ID' => $siteID,
'service' => $services
];
}
?>
Change the way you generate your array to something like this:
<?php
$data = [
'sites' => [],
];
foreach ($_POST['services'] as $item) {
// Gather your data
$parts = explode(',', $item);
$siteID = $parts[1];
$services = $parts[0];
// Set flag to remember whether you need to add it
$add = true;
// Loop through existing data and check if the sys_ID already exists
foreach ($data['sites'] as &$dataDetails) {
// It does ... so just append your service
if ($dataDetails['sys_ID'] === $siteID) {
$add = false;
$dataDetails['service'] .= ',' . $services;
break;
}
}
// Couldn't find the sys_ID => Add new entry
if ($add) {
$data['sites'][] = [
'sys_ID' => $siteID,
'service' => $services
];
}
}
?>
It will be better to fill the array properly from the beginning; however I will still post a solution. We have the array defined like this:
$arr = array(
'sys_ID' => 'ab0ce921dba8a810f6db3892399619d9',
'sites' => array(
0 => array(
'sys_ID' => "448ce5a1dba8a810f6db3892399619ba",
'service'=> "IDMB"
),
1 => array(
'sys_ID' => "448ce5a1dba8a810f6db3892399619ba",
'service'=> "ODMB"
),
2 => array(
'sys_ID'=> "598ce5a1dba8a810f6db3892399619bc",
'service'=> "IDMB"
),
3 => array(
'sys_ID'=> "876ce5a1dba8a810f6db38923996199f",
'service'=> "IDMB"
),
4 => array(
'sys_ID'=> "876ce5a1dba8a810f6db38923996199f",
'service'=> "ODMB"
)
)
);
To merge the elements like described, you can do the following loop:
// For each site (1)...
for($i=0; $i<count($arr["sites"])-1; $i++){
// Take each of sites starting from the next one to the end (2)
for($j=$i+1; $j<count($arr["sites"]); $j++){
// If there is a match in sys_ID beteen (1) and (2)
if($arr["sites"][$i]['sys_ID'] == $arr["sites"][$j]['sys_ID']){
// Merge the service into (1) with comma separation
$arr["sites"][$i]['service'] = $arr["sites"][$i]['service'].",".$arr["sites"][$j]['service'];
// then delete (2)
array_splice($arr["sites"],$j,1);
$j--;
}
}
}
Note that this will reindex the numebers (1,2,3...). If you wish to preserve them, you can use unset() instead.

Combining array values for the same associative key

I've got this array, and I want to loop through it and add up the values prices that are on the same OrderDate. The other values like the Discount code I want to add as a sub-array.
array(3) {
[0]=>
array(4) {
["OrderDate"]=>
string(10) "2018-01-01"
["DiscountCode"]=>
NULL
["TotalRevenue"]=>
string(9) "147618.76"
["Discount_Revenue"]=>
string(8) "13453.77"
}
[1]=>
array(4) {
["OrderDate"]=>
string(10) "2018-01-01"
["DiscountCode"]=>
string(6) "SALE38"
["TotalRevenue"]=>
string(8) "364.92"
["Discount_Revenue"]=>
string(8) "4083.64"
}
[2]=>
array(4) {
["OrderDate"]=>
string(10) "2018-01-01"
["DiscountCode"]=>
string(9) "WELCOME20"
["TotalRevenue"]=>
string(6) "113.83"
["Discount_Revenue"]=>
string(6) "113.83"
}
}
So it should then look like:
array(3) {
[0]=>
array(4) {
["OrderDate"]=>
string(10) "2018-01-01"
["DiscountCodes"]=> array {
[0] => "DISCOUNT"
[1] => "SALE38"
[2] => "WELCOME20"
)
["TotalRevenue"]=>
string(9) "147618.76"
["Discount_Revenue"]=>
string(8) "13453.77"
}
}
I believe I have fixed it using this loop adding to the array if the key exists. Not sure if this is the most efficient way to do it though?
foreach ($results as $k => $result){
if( array_key_exists($result['OrderDate'], $arr)){
$arr[$result['OrderDate']]['price'] += $result['TotalRevenue'];
$arr[$result['OrderDate']]['new'] = false;
} else {
$arr[$result['OrderDate']] = array(
'price' => $result['TotalRevenue'],
'new' => true
);
}
}
I've come to my own solution if anyone else needs it.
$arr = array();
foreach ($results as $k => $result){
if( array_key_exists($result['OrderDate'], $arr)){
$arr[$result['OrderDate']]['Total_Revenue'] += $result['TotalRevenue'];
$arr[$result['OrderDate']]['Discount_Revenue'] += $result['Discount_Revenue'];
isset($result['DiscountCode']) ? $arr[$result['OrderDate']]['Discount_Code'][] = $result['DiscountCode'] : '';
$arr[$result['OrderDate']]['new'] = false;
} else {
$arr[$result['OrderDate']] = array(
'Total_Revenue' => $result['TotalRevenue'],
'Discount_Revenue' => $result['Discount_Revenue'],
'new' => true
);
isset($result['DiscountCode']) ? $arr[$result['OrderDate']]['Discount_Code'][] = $result['DiscountCode'] : '';
}
}

PHP - Push to array without key

I'm trying to add some $_POST values to my $_SESSION array, but I can't seem to find the best approach.
Result with array_push():
array(1) {
'product_ids' =>
array(2) {
[0] =>
string(1) "9"
[1] =>
array(1) {
[0] =>
string(2) "14"
}
}
}
expected result:
array(1) {
'product_ids' =>
array(2) {
[0] =>
string(1) "9"
[1] =>
string(2) "14"
}
}
}
Code:
if(!empty($_SESSION['product_ids'])){
array_push($_SESSION['product_ids'],$_POST['id']);
} else {
$_SESSION['product_ids'] = $_POST['id'];
}
$_POST array:
array(1) {
'id' =>
array(1) {
[0] =>
string(2) "14"
}
}
$_SESSION array:
Emtpy
If $_POST['id'] is an array of ids, it should be named $_POST['ids']. Use clear naming.
... else {
$_SESSION['product_ids'] = $_POST['id'];
Initially you're creating $_SESSION['product_ids'] as an array…
if(!empty($_SESSION['product_ids'])){
array_push($_SESSION['product_ids'],$_POST['id']);
…you're then pushing an array as value into the existing array. You want to merge those two arrays into a new array instead:
if (empty($_SESSION['product_ids'])) {
$_SESSION['product_ids'] = $_POST['ids'];
} else {
$_SESSION['product_ids'] = array_merge($_SESSION['product_ids'], $_POST['ids']);
}

Find object key in array using value inside an array inside the object PHP

I have an object array built like this (output of a "var_dump()" call i sanitized for the question a bit):
sObjects:array(3) {
[0]=>
object(SObject)#1 (2) {
["type"]=>
string(9) "Course__c"
["fields"]=>
array(1) {
["Id__c"]=>
string(3) "111"
}
}
[1]=>
object(SObject)#2 (2) {
["type"]=>
string(9) "Course__c"
["fields"]=>
array(1) {
["Id__c"]=>
string(3) "222"
}
}
[2]=>
object(SObject)#3 (2) {
["type"]=>
string(9) "Course__c"
["fields"]=>
array(1) {
["Id__c"]=>
string(3) "333"
}
}
}
Now, lets say i have $id = "111"
How would i go about iterating over my object array and retrieve the array key where [id__c] has a value equal to $id?
for example in this case i would expect to get back 0.
Use array_filter like this:
$array = [
[
"type" => "Course__c",
"fields" => ["Id_c" => "111"]
],
[
"type" => "Course__c",
"fields" => ["Id_c" => "222"]
]
];
$result = array_filter($array,
function($element) {
return $element['fields']['Id_c'] == "111" ? true :false;
});
print_r($result);
Will output:
Array
(
[1] => Array
(
[type] => Course__c
[fields] => Array
(
[Id_c] => 111
)
)
)
For the Sobject version, replace $element['fields']['Id_c'] with $element->fields['Id_c']
Also if you would like to pass a variable inside the callback function use:
$result = array_filter($array,
function($element) use($externalVariable){
return $element['fields']['Id_c'] == $externalVariable ? true :false;
});

Sort array by the value of the key

i'm trying to sort an array by the value of a sub-key in DESC order but I'm stuck.
I've could make it with ksort but it was in ascending order..
Here's my array :
array_by_lang => array(
[no] => array(
[3-1] => array(//some informations),
[3-10] => array(//informations),
[3-7] => array(//informations),
[5-1] => array(//informations)
)
)
what i want to obtain is something like :
array_by_lang => array(
[no] => array(
[5-1] => array(//informations),
[3-10] => array(//some informations),
[3-7] => array(//informations),
[3-1] => array(//informations)
)
)
Is that possible ? Thanks a lot
I think, you need "reversing natural sort by key". Just with array_multisort and array_reverse (see also natsort):
$array_by_lang = array(
'no' => array(
'3-1' => array('info_1'),
'3-10' => array('info_2'),
'3-7' => array('info_3'),
'5-1' => array('info_4'),
)
);
array_multisort(array_keys($array_by_lang['no']),SORT_NATURAL, $array_by_lang['no']);
$array_by_lang['no'] = array_reverse($array_by_lang['no']); // reverse natural order - "DESC"
var_dump($array_by_lang);
Output
array(1) {
["no"]=>
array(4) {
["5-1"]=>
array(1) {
[0]=>
string(6) "info_4"
}
["3-10"]=>
array(1) {
[0]=>
string(6) "info_2"
}
["3-7"]=>
array(1) {
[0]=>
string(6) "info_3"
}
["3-1"]=>
array(1) {
[0]=>
string(6) "info_1"
}
}
}
This might help -
$a = array(
'3-1' => array('//some informations'),
'3-10' => array('//informations'),
'3-7' => array('//informations'),
'5-1' => array('//informations')
);
## Array for keys
$temp= array();
foreach(array_keys($a) as $v) {
$t = explode('-', $v);
$temp[$t[0]][] = $t[1];
}
## Sort the keys
foreach($temp as &$ar) {
rsort($ar);
}
krsort($temp);
## Final array
$final= array();
foreach($temp as $k => $f) {
foreach($f as $v) {
$key = $k . '-' . $v;
$final[$key] = $a[$key];
}
}
var_dump($final);
Output
array(4) {
["5-1"]=>
array(1) {
[0]=>
string(14) "//informations"
}
["3-10"]=>
array(1) {
[0]=>
string(14) "//informations"
}
["3-7"]=>
array(1) {
[0]=>
string(14) "//informations"
}
["3-1"]=>
array(1) {
[0]=>
string(19) "//some informations"
}
}
DEMO

Categories