How to map multidimensional array - php

I have an array of arrays that I need to consolidate into another array.
I have tried mapping over it, matching object_id, and gathering all account_ids for said object_id, but all my attempts are not even close as I am trying to learn PHP
This is the original array
[0] => Array
(
[rank] => 0
[id] => 6
[object_id] => 3
[account_id] => 13
)
[1] => Array
(
[rank] => 1
[id] => 7
[object_id] => 3
[account_id] => 565
)
[2] => Array
(
[rank] => 2
[id] => 1823
[object_id] => 825
[account_id] => 563
)
[3] => Array
(
[rank] => 3
[id] => 1824
[object_id] => 825
[account_id] => 564
)
[4] => Array
(
[rank] => 4
[id] => 1825
[object_id] => 825
[account_id] => 565
)
[5] => Array
(
[rank] => 5
[id] => 7187
[object_id] => 3113
[account_id] => 564
)
[6] => Array
(
[rank] => 6
[id] => 7188
[object_id] => 3113
[account_id] => 565
)
This is the desired result
[3] => [13, 565],
[825] => [563, 564, 565],
[3113] => [564, 565],

You need to create a new array by using object_id index.
Example:
<?
$array = array(
array('rank'=>0,'id'=>6,'object_id'=>3,'account_id'=>13),
array('rank'=>1,'id'=>7,'object_id'=>3,'account_id'=>565),
array('rank'=>2,'id'=>1823,'object_id'=>825,'account_id'=>563),
array('rank'=>3,'id'=>1824,'object_id'=>825,'account_id'=>564),
array('rank'=>4,'id'=>1825,'object_id'=>825,'account_id'=>565),
array('rank'=>5,'id'=>7187,'object_id'=>3113,'account_id'=>564),
array('rank'=>6,'id'=>7188,'object_id'=>3113,'account_id'=>565),
);
$newArray = array(); // initiliaze array
foreach ($array as $key => $value) {
$newArray[$value['object_id']][] = $value['account_id']; // save it in group
}
echo "<pre>";
print_r($newArray); // result
?>
Running Example

Related

Change structure multidimensional array

Good day. I am new at php and I have a small problem. I have an array.. I want to change stucture of it .. object_type sometimes the same.. I want to have multidimensional array with object_type a key and value all arrays with this object_type.
[0] => Array
(
[initiator_id] => 259
[object_type] => 1
[object_id] => 905
[date] => 2021-11-16 06:24:16
)
[1] => Array
(
[initiator_id] => 259
[object_type] => 1
[object_id] => 905
[date] => 2021-11-16 04:54:54
)
[2] => Array
(
[initiator_id] => 259
[object_type] => 1
[object_id] => 905
[date] => 2021-11-16 04:53:58
)
[3] => Array
(
[initiator_id] => 219
[object_type] => 2
[object_id] => 915
[date] => 2021-11-16 04:53:58
)
Here you can find how to create Multidimensional Arrays. Click here PHP Multidimensional Arrays
And to learn PHP language www.w3schools.com is a very good site. You can find almost every example briefly explained here.
I hope this example may solve your issue.
<?php
$obj = array("initiator_id"=>"35", "object_type"=>"37", "object_id"=>"43");
$obj_2 = array("initiator_id"=>"135", "object_type"=>"237", "object_id"=>"343");
$array_list = array (
array("Test 1",22,18),
array("Test 2",5,2),
array("Test 3",17,15)
);
$array_list[1]["new"] = $obj_2;
$result = array_merge($array_list, $obj);
echo "<Pre>";
print_r($result);
echo "</pre>";
?>
Output:
Array
(
[0] => Array
(
[0] => Test 1
[1] => 22
[2] => 18
)
[1] => Array
(
[0] => Test 2
[1] => 5
[2] => 2
[new] => Array
(
[initiator_id] => 135
[object_type] => 237
[object_id] => 343
)
)
[2] => Array
(
[0] => Test 3
[1] => 17
[2] => 15
)
[initiator_id] => 35
[object_type] => 37
[object_id] => 43
)

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

extracting an element of an array [duplicate]

This question already has answers here:
Get index of row with qualifying value from a 2d array
(5 answers)
Closed 7 years ago.
Assume this array:
Array ( [0] => Array ( [id] => 1171 [product_id] => 140 [fileid] => 479717 [purchid] => 847 [cartid] => 833 [uniqueid] => f100c3b3a853202fb6559fbacf025a6aa07f52c7 [downloads] => 99998 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[1] => Array ( [id] => 1172 [product_id] => 140 [fileid] => 313624 [purchid] => 847 [cartid] => 833 [uniqueid] => f00a3c91378ad469f333abeec64753b275f10670 [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[2] => Array ( [id] => 1173 [product_id] => 140 [fileid] => 313618 [purchid] => 847 [cartid] => 833 [uniqueid] => ac125595e2dbca6a086261434582f6e7dfc5638e [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[3] => Array ( [id] => 1174 [product_id] => 140 [fileid] => 313526 [purchid] => 847 [cartid] => 833 [uniqueid] => 3e6123e0a4453de71dec91a177f5b34217625680 [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 ))
I want to "extract" the [0] array and use it for something else BUT it has to be conditioned.
I said the 0 element because it has the fileid = 479717 the one that i want.
So i`m looking at extracting the array (in my case [0]) that has filed = $myvalue. Where i can set $myvalue to whatever i want.
Does this fits your needs ?
function findById($id, $arrayOfArrays){
foreach ( $arrayOfArrays as $contents )
if ( $contents['id'] == $id ) return $contents;
}
function filter($fileid, array $array) {
foreach ($array as $key => $value) {
if ($value['fileid'] === $fileid) {
return $value;
}
}
}

PHP array_merge not doing exactly what i need it to do

Ok so here is my first array:
(
[1] => Array
(
[27] => Array
(
[product_id] => 27
[type] => hardware
[step_number] => 1
)
[372] => Array
(
[product_id] => 372
[type] => hardware
[step_number] => 1
)
[92] => Array
(
[product_id] => 92
[type] => hardware
[step_number] => 1
)
)
[2] => Array
(
[335] => Array
(
[product_id] => 335
[type] => hardware
[step_number] => 2
)
[62] => Array
(
[product_id] => 62
[type] => hardware
[step_number] => 2
)
[356] => Array
(
[product_id] => 356
[type] => hardware
[step_number] => 2
)
)
and here is my second array
(
[1] => Array
(
[655] => Array
(
[product_id] => 655
[type] => optional
[step_number] => 1
)
[54] => Array
(
[product_id] => 54
[type] => optional
[step_number] => 1
)
[554] => Array
(
[product_id] => 554
[type] => optional
[step_number] => 1
)
)
[2] => Array
(
[33] => Array
(
[product_id] => 33
[type] => optional
[step_number] => 2
)
[612] => Array
(
[product_id] => 612
[type] => optional
[step_number] => 2
)
[5] => Array
(
[product_id] => 5
[type] => optional
[step_number] => 2
)
)
[3] => Array
(
[444] => Array
(
[product_id] => 444
[type] => optional
[step_number] => 3
)
[6] => Array
(
[product_id] => 6
[type] => optional
[step_number] => 3
)
[53] => Array
(
[product_id] => 53
[type] => optional
[step_number] => 3
)
)
Basically what i need is the second array appended to the end of the first array with the keys of the first array preserved the keys and changing the step_number to the new key so the final keys looks like
(
[1] => Array
(
[27] => Array
(
[step_number] => 1)
....
[2] => Array
(
[335] => Array
(
[step_number] => 2)
....
[3] => Array
(
[655] => Array
(
[step_number] => 3)
....
[4] => Array
(
[33] => Array
(
[step_number] => 4)
....
[5] => Array
(
[444] => Array
(
[step_number] => 5)
....
but when i do
$all = array_merge($first, $second);
the keys are
(
[0] => Array
[1] => Array
[2] => Array
[3] => Array
[4] => Array
Is that possible to do....
Try array_merge_recursive .. http://php.net/array_merge_recursive
This is also from the manual on array_splice if you're just looking to append your 2nd array onto the first (http://php.net/manual/en/function.array-push.php):
array_splice($first, count($first), 0, $second);
Iterating over the first set of keys, then using the Union array operator should do the trick.
//$first = array(...);
//$second = array(...);
foreach ($second as $key => $value)
{
if (!isset($first[$key]))
{
$first[$key] = array();
}
$first[$key] += $value;
}

Removing an array result

[5] => Array
(
[id] => 29372
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1469
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 21cf9d7d09d403251ba5d01ff33cd089.jpg
[coverid] => 1178
[inserted_by] => 0
[inserted_date] => 2011-02-11 13:55:23
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1469
[media_format] => VCD
[price] => 29000
[discount] => 5
)
)
[media_format] => VCD
)
[6] => Array
(
[id] => 30074
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1470
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 149ddd4d1d5e567c1300d4831323e1c5.jpg
[coverid] => 1177
[inserted_by] => 6
[inserted_date] => 2011-02-16 15:18:58
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1470
[media_format] => DVD
[price] => 39000
[discount] => 0
)
)
[media_format] => DVD
)
I want the result by media_format = DVD so the whole array is gone, is there anyway to delete an array or remove it?
The same way you "delete" any variable:
unset($array[$index]);

Categories