I have the below array, that needs some additional structuring.
Array
(
[-Others] => Array
(
[0] => Array
(
[products_sold] => 1
[products_total_sales] => 2.99
[products_total_costs] => 1.75
[products_total_profit] => 1.24
)
[1] => Array
(
[products_sold] => 1
[products_total_sales] => 2.3322
[products_total_costs] => 1.75
[products_total_profit] => 0.5822
)
)
[Addict] => Array
(
[0] => Array
(
[products_sold] => 1
[products_total_sales] => 35.1
[products_total_costs] => 40
[products_total_profit] => -4.9
)
I have truncated this early, obviously. I was wondering if it would be possible to add up all the sales for all child products, the products_total_sales per brand (aka addict)
I would like to get it in before they are parsed so something like:
[Addict] => Array
(
[0] => Array
[brand_total_sales] => '$value'
(
[products_sold] => 1
...
Any help is greatly appreciated. Thanks in advance and feel free to ask if you need more information
Well, in case the answer is yes:
foreach($array as $brand_name => $brand_array) {
$array[$brand_name]['brand_total_sales'] = 0;
foreach($brand_array as $product) {
$array[$brand_name]['brand_total_sales'] += $product['products_total_sales'];
}
}
Related
I'm trying to return the LARGEST numerical value associated with the 'reduced' array below. Easy enough if there is one value but many have two- as below. I'm using
$reduced_array = $data['rates'][1]['rates'][0];
but this only works in returning the first value. I need to return just the highest value however- so below it would be 8.
would something like
if(count($data['rates'][1]['rates']) > 2) {
***return largest value here***
work? I'm just not sure how to perform the asterisked task- maybe a for loop? here is the array.
Array
(
[rates] => Array
(
[0] => Array
(
[name] => Super Reduced
[rates] => Array
(
)
)
[1] => Array
(
[name] => Reduced
[rates] => Array
(
[0] => 5
[1] => 8
)
)
[2] => Array
(
[name] => Standard
[rates] => Array
(
[0] => 23
)
)
[3] => Array
(
[name] => Increased
[rates] => Array
(
)
)
[4] => Array
(
[name] => Parking
[rates] => Array
(
)
)
)
[disclaimer] => Rates data is based on information published by the European Commission, updated 1st January 2017.
)
Thanks for any help
You can use max() for that.
$array = array(
"rates" => array(
array(
"name" => "Super Reduced",
"rates" => array()
),
array(
"name" => "Reduced",
"rates" => array(
5,
8
)
)
)
);
echo max($array["rates"][1]["rates"]); // 8
Easiest solution will be using of max on inner array
example:
if(!empty($data['rates'][1]['rates'])) {
$maxvalue = max($data['rates'][1]['rates']);
}
I have an array which is as follows:
Array
(
[0] => Array
(
[postId] => 105
[postTitle] => Test
[postNonArray] => Subzero
[postDesc] => Array
(
[0] => Array
(
[para] => Subzero
[align] => L
)
)
[postDate] => 25.08.2016
[postTime] => 13:44
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472112857.png
[postVideo] =>
)
[1] => Array
(
[postId] => 106
[postTitle] => Test 2
[postNonArray] => Test
[postDesc] => Array
(
[0] => Array
(
[para] => Test
[align] => L
)
)
[postDate] => 26.08.2016
[postTime] => 18:08
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472215085.jpg
[postVideo] =>
)
[2] => Array
(
[postId] => 106
[postTitle] => Test 2
[postNonArray] => Test
[postDesc] => Array
(
[0] => Array
(
[para] => Test
[align] => L
)
)
[postDate] => 26.08.2016
[postTime] => 18:08
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472215085.jpg
[postVideo] =>
)
)
As you can see, there is two post details with postId=106;
How can I remove the redundant data from the array based on postId?
The project is on PHP.
I think this is what you are trying to achieve:-
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
echo "<pre/>";print_r($array);
Check output(whole code with your original array):- https://eval.in/630678
Note:- It will remove the duplicate values (so whole duplicate array will gone as you asked in comment)
I would suggest loop like the one below. It will go through all the elements from $your_array_name and will make an unique array of id where we will store the postIds. We will also check if there are duplicated in the $unique_ids array, and if so we will remove that duplicate element.
$unique_ids = array();
foreach($your_array_name as $key => $value){
//check if the postId is in the array of the unique ids
if(!in_array($value['postId'], $unique_ids)()){
array_push($unique_ids,$value['postId']); //if it is not - push it there
} else {
unset($your_array_name($key)); //if it is - remove the whole element from the array
}
}
You will need to loop the data and create a new array with unique values so here you go:
$ShowNewArray = array();
foreach($array as $key => $value){
if(!array_key_exists('postId', $ShowNewArray)){
$ShowNewArray[$value['postId']] = $value;
}
}
print_r($ShowNewArray);
Hope it will help you.
I have a multidimensional array that is displayed to users in a table, where they can select items by a checkbox.
When they've checked their items and submit, I've now got an array of id values that correspond to the myid key of the original sub arrays.
How can I search the original array and create a new array of only the matching selected items?
Array (
[0] => Array (
[myid] => 22
[Price] => Some price
[Title] => Some text
)
[1] => Array (
[myid] => 36
[Price] => Some price
[Title] => Some text
)
)
Any help would be greatly appreciated!
Simple way but can be optimized
<?php
$submittedVaule = array('12','14');
$subArray = array(0 => array('myid' => 12,'price' => '100','title' => 'test1'),1 => array('myid' => 13,'price' => '100','title' => 'test2'),2 => array('myid' => 14,'price' => '100','title' => 'test3'));
$finalarray = array();
foreach($subArray as $key=>$value){
if(in_array($value['myid'], $submittedVaule )) {
$finalarray[]=$subArray[$key];
}
}
print_r($finalarray);
?>
I have this multidimensional array, called $rent:
Array
(
[product2] => Array
(
[dates] => Array
(
[2013-07-25] => 2
[2013-07-23] => 1
[2013-07-21] => 3
)
)
[product3] => Array
(
[dates] => Array
(
[2013-07-24] => 5
[2013-07-22] => 4
[2013-07-20] => 3
)
)
[product1] => Array
(
[dates] => Array
(
[2013-07-29] => 1
[2013-07-28] => 2
[2013-07-27] => 2
)
)
)
I'd like to do a double sort:
First, by productX ascending
Then, for each product, by date of rent ascending
So that the resulting array would be:
Array
(
[product1] => Array
(
[dates] => Array
(
[2013-07-27] => 2
[2013-07-28] => 2
[2013-07-29] => 1
)
)
[product2] => Array
(
[dates] => Array
(
[2013-07-21] => 3
[2013-07-23] => 1
[2013-07-25] => 2
)
)
[product3] => Array
(
[dates] => Array
(
[2013-07-20] => 3
[2013-07-22] => 4
[2013-07-24] => 5
)
)
)
How can I reach this? Many thanks in advance
try this:
ksort($rent);
foreach($rent as &$item) {
ksort($item['dates']);
}
You can simply ksort the products, then iterate through them and use the same for the dates key.
ksort($products);
foreach($products as &$product)
ksort($product['dates']);
Where $products is the array you showed us. Note that you need to pass the value in the foreach loop as a reference (using the & operator) otherwise the changes won't be updated in the original array.
for my understanding of your problem; Nadh solution is almost there. but i believe you want ksort()
this is my corrections to Nadh answer
ksort($rent);
foreach($rent as $product => $dates) {
ksort($rent[$product]['dates']);
}
print_r($rent);
I have an associative array that looks like this:
Array (
[0] => Array (
[amount] => 3
[name] => Chuck
)
[1] => Array (
[amount] => 2
[name] => Steve
)
[2] => Array (
[amount] => 5
[name] =>
)
[3] => Array (
[amount] => 4
[name] => Chuck
)
[4] => Array (
[amount] =>
[name] => Chuck
)
)
I need to remove values that are missing a name or amount e.g. [2] and [4] and then sum the totals for each name so that the final array is:
Array (
[0] => Array (
[amount] => 7
[name] => Chuck
)
[1] => Array (
[amount] => 2
[name] => Steve
)
)
For anyone looking for this nowadays, this would be much cleaner:
$sum = array_sum(array_column($data, 'amount'));
Try this:
$starting_array = array( ... ); // Initial array with your setup
$final_array = array();
$sum = 0;
foreach ($starting_array as $idx => $data) {
if (!empty($data['amount']) && !empty($data['name'])) {
$final_array[$idx] = $data;
$sum += $data['amount'];
}
}
// After looping through all of the items, $final_array should contain all
// of the specific items that have an amount and name set. In addition, the
// total of all of the amounts will be in $sum.
Take a look at php's empty(). Note: If 0 is an allowed value, you may want to use is_null() instead.