How do I access data within this multidimensional array? - php

I have this array:
$items_pool = Array (
[0] => Array ( [id] => 1 [quantity] => 1 )
[1] => Array ( [id] => 2 [quantity] => 1 )
[2] => Array ( [id] => 72 [quantity] => 6 )
[3] => Array ( [id] => 4 [quantity] => 1 )
[4] => Array ( [id] => 5 [quantity] => 1 )
[5] => Array ( [id] => 7 [quantity] => 1 )
[6] => Array ( [id] => 8 [quantity] => 1 )
[7] => Array ( [id] => 9 [quantity] => 1 )
[8] => Array ( [id] => 19 [quantity] => 1 )
[9] => Array ( [id] => 20 [quantity] => 1 )
[10] => Array ( [id] => 22 [quantity] => 1 )
[11] => Array ( [id] => 29 [quantity] => 0 )
)
I'm trying to loop through this array and perform a conditional based on $items_pool[][id]'s value. I want to then report back TRUE or NULL/FALSE, so I'm just testing the presence of to be specific.

Something like this:
$items_pool = array(...);
$result = false;
foreach ($items_pool as $item) {
if ('something' == $item['id']) {
$result = true;
break;
}
}

Loop through an check if anything is empty..
foreach($items_pool as $arr){
echo $arr['id'].'==>'.$arr['quantity'];
if($arr['quantity'] == 0){
echo 'id:'.$arr['id'].' is empty!';
return false;
}
}

Related

Php creating associative array from another array

I am using an array that I get from a database and I would like to create an associative array from that one.
Here is the code I am using:
print_r($data_sites);
// Format for easy use
$data_sites_formatted = array();
foreach ($data_sites as $key => $row) {
if (empty($data_formatted[$row->project_loe_id])) {
$data_sites_formatted[$row->project_loe_id] = array();
}
$data_sites_formatted[$row->project_loe_id][$row->name] = array();
$data_sites_formatted[$row->project_loe_id][$row->name]['id'] = $row->id;
$data_sites_formatted[$row->project_loe_id][$row->name]['quantity'] = $row->quantity;
$data_sites_formatted[$row->project_loe_id][$row->name]['loe_per_quantity'] = $row->loe_per_quantity;
}
print_r($data_sites_formatted);
Here is the information I receive from the database:
Illuminate\Support\CollectionObject ( [items:protected] => Array (
[0] => stdClass Object ( [id] => 1 [project_loe_id] => 1 [name] => site [quantity] => 20 [loe_per_quantity] => 1 )
[1] => stdClass Object ( [id] => 2 [project_loe_id] => 1 [name] => switches [quantity] => 5 [loe_per_quantity] => 3 )
[2] => stdClass Object ( [id] => 3 [project_loe_id] => 2 [name] => site [quantity] => 20 [loe_per_quantity] => 1 )
[3] => stdClass Object ( [id] => 4 [project_loe_id] => 2 [name] => ap [quantity] => 5 [loe_per_quantity] => 3 )
[4] => stdClass Object ( [id] => 5 [project_loe_id] => 2 [name] => wireless [quantity] => 5 [loe_per_quantity] => 3 ) ) )
And when I do the transformation, here is the result:
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [wireless] => Array ( [id] => 5 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
I lost 3 lines and I really have no idea why. I can see when I look into details step by step that it deletes some lines from 1 iteration to the next:
Iteration:0
Array (
[1] => Array ( [site] => Array ( [id] => 1 [quantity] => 20 [loe_per_quantity] => 1 ) ) )
Iteration: 1
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
Iteration: 2
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [site] => Array ( [id] => 3 [quantity] => 20 [loe_per_quantity] => 1 ) ) )
Iteration: 3
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [ap] => Array ( [id] => 4 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
Iteration: 4
Array (
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) )
[2] => Array ( [wireless] => Array ( [id] => 5 [quantity] => 5 [loe_per_quantity] => 3 ) ) )
You can see from iteration 0 to iteration 1, it deleted the line from iteration 0 and in iteration 1 I should have 2 lines.
You have a typo here:
if (empty($data_formatted[$row->project_loe_id])) {
$data_sites_formatted[$row->project_loe_id] = array();
}
$data_formatted is never defined (you meant to write $data_sites_formatted) so empty($data_formatted[$row->project_loe_id]) is always true, and always runs the next line, replacing anything previously added to $data_sites_formatted[$row->project_loe_id] with an empty array. So all you end up with is the last item to be added in each group.

PHP: Find elements in deep array, then modify each parent element

I have a varying level of deepness for a multidimensional array. I want to find the elements where UserId isset (see example array below), then add a key/value pair ([show] => true) to the matched element and each parent array. There may be multiple matches that will need to modify the parents.
I have this:
Array
(
[0] => Array
(
[id] => 3
[parent_id] => 0
[ownerEntityId] => 2
[children] => Array
(
[0] => Array
(
[id] => 15
[parent_id] => 3
[ownerEntityId] => 14
[children] => Array
(
[0] => Array
(
[id] => 17
[parent_id] => 15
[ownerEntityId] =>
[userId] => 2
)
[1] => Array
(
[id] => 18
[parent_id] => 15
[ownerEntityId] =>
)
[2] => Array
(
[id] => 19
[parent_id] => 15
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 11
[parent_id] => 3
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 26
[parent_id] => 1
[ownerEntityId] =>
[children] => Array
(
[0] => Array
(
[id] => 23
[parent_id] => 26
[ownerEntityId] => 24
[1] => Array
(
[id] => 41
[parent_id] => 26
[ownerEntityId] =>
)
)
)
I want this:
Array
(
[0] => Array
(
[id] => 3
[parent_id] => 0
[ownerEntityId] => 2
[show] => true //***Added
[children] => Array
(
[0] => Array
(
[id] => 15
[parent_id] => 3
[ownerEntityId] => 14
[show] => true //***Added
[children] => Array
(
[0] => Array
(
[id] => 17
[parent_id] => 15
[ownerEntityId] =>
[show] => true //***Added
[userId] => 2
)
[1] => Array
(
[id] => 18
[parent_id] => 15
[ownerEntityId] =>
)
[2] => Array
(
[id] => 19
[parent_id] => 15
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 11
[parent_id] => 3
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 26
[parent_id] => 1
[ownerEntityId] =>
[children] => Array
(
[0] => Array
(
[id] => 23
[parent_id] => 26
[ownerEntityId] => 24
[1] => Array
(
[id] => 41
[parent_id] => 26
[ownerEntityId] =>
)
)
)
I have been messing with multiple recursion functions that have failed me miserably.
https://ideone.com/HoJPry
function hasUserId (&$el) {
if(isset($el['children'])) {
$ret = false;
foreach($el['children'] as &$child) {
if(hasUserId($child)) {
$ret=true;
}
}
if($ret) {
$el['show']=true;
return true;
}
} ;
if (isset($el['user_id'])) {
$el['show']=true;
return true;
}
}
foreach($top as $i => $t)
{
foreach($t['children'] as $n => $mid)
{
foreach($mid['children'] as $x => $bottom)
{
if($bottom['id'] !== $target_id)
continue;
$top[$i]['show'] = true;
$top[$i]['children'][$n]['show'] = true;
$top[$i]['children'][$n]['children'][$x]['show'] = true;
}
}
}

how to make incremental value in hierarchal array in Php?

Hi guys I was wondering how can I add a incremental all elements? Because as of now I am not sure where can I include the "inc" in all elements
Ex:
MY_ARRAY = (
[id] => 4
[children] => Array
(
[0] => Array
(
[id] => 18
[children] => Array
(
[0] => Array
(
[id] => 21
)
[1] => Array
(
[id] => 22
)
)
)
[1] => Array
(
[id] => 19
)
[2] => Array
(
[id] => 20
[children] => Array
(
[0] => Array
(
[id] => 26
)
)
)
)
)
Using these code:
$in = MY_ARRAY
function generateArray($in, $parent = 0){
foreach ($in as $key => $value) {
if(is_numeric($key)){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}else{
$out[$key]=$value;
if($key=="id"){
$out['p_id'] = $parent;
$parent=$value;
}elseif($key=="children"){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}
}
}
return $out;
}
Will give me this output, not including the [inc].
[id] => 4
[P_id] => 0
[inc] => 1
[children] => Array
(
[0] => Array
(
[id] => 18
[P_id] => 4
[inc] => 2
[children] => Array
(
[0] => Array
(
[id] => 21
[P_id] => 18
[inc] => 3
)
[1] => Array
(
[id] => 22
[P_id] => 18
[inc] => 4
)
)
)
[1] => Array
(
[id] => 19
[P_id] => 4
[inc] => 5
)
[2] => Array
(
[id] => 20
[P_id] => 4
[inc] => 6
[children] => Array
(
[0] => Array
(
[id] => 26
[P_id] => 20
[inc] => 7
)
)
)
)
)
Now I'm not sure where and how can I include the [inc] or the incremental value of each element in the array using my code above.
Need really help here guys...

How to reformat an array using php?

I have the following array:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 50
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[1] => Array
(
[Import] => Array
(
[id] => 2
[category_id] => 2
[product_id] => 2
[amount] => 15
[cost] => 3000
[comment] =>
[created] => 2015-06-22 18:10:36
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[2] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
I want to remove duplicate value of same product_id inside [Import][product_id] but want sum [Import][amount]. My expected array is:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 65
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[1] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
It will be really a gift if anyone give a function to solve this issue.
$filteredArray = [];
foreach ($array as $productData) {
if (isset($filteredArray[$productData['Import']['product_id']])) {
$filteredArray[$productData['Import']['product_id']]['Import']['amount'] += $productData['Import']['amount'];
}
else {
$filteredArray[$productData['Import']['product_id']] = $productData;
}
}
print_r($filteredArray);
Ah.. forgot to mention - $array is Your base array.
/**
* Removes duplicate summing amount from products array
*
* #param array $array
* #return array
*/
function removeDuplicates($array)
{
$idsCount = array();
foreach ($array as $key => $value) {
$idsCount[$value['Import']['product_id']]['count'] += 1;
$idsCount[$value['Import']['product_id']]['sum'] += $value['Import']['amount'];
if ($idsCount[$value['Import']['product_id']]['count'] > 1) {
unset($array[$key]);
$array[$idsCount[$value['Import']['product_id']]['key']]['Import']['amount'] = $idsCount[$value['Import']['product_id']]['sum'];
} else {
$idsCount[$value['Import']['product_id']]['key'] = $key;
}
}
return $array;
}
i know it looks crazy but is formatted on your specific array.

How to remove duplicate data in an array?

I have the following array:
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
[2] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 3
[category_id] => 1
[amount] => 15
[cost] => 2000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:58
)
[0] => Array
(
[total_sell] => 6
)
)
[3] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 4
[category_id] => 1
[amount] => 50
[cost] => 8000
[paid] => 0
[comment] =>
[created] => 2015-06-23 01:10:10
)
[0] => Array
(
[total_sell] => 6
)
)
)
I want to remove duplicate entry of [Import][product_id]. So my expected result is :
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
)
Would you write a function to filter this type of array and produce expected result. I have been googling for 2 days but no luck.
This is a handy one liner that should do the trick:
$unique= array_map("unserialize", array_unique(array_map("serialize", $original)));
If the underlying arrays are not identical, that won't work, in which case I think you could do:
$unique = array_intersect_key($original ,
array_unique(
array_map(function($item) {
return $item['Import']['product_id'];
}, $original)
)
);
Tested: http://sandbox.onlinephpfunctions.com/code/8aee5cbd614e0ddd1a03dfaa7e98c72fbbe7d68d
Here is a quick stable sort and reduce which runs in linearithmic time. First-encountered product Id's are kept, and entries with duplicate product Id's are ignored.
// Stable sort
sort($in);
// Reduce
$out = array_reduce($in, function(&$acc, &$item){
if($item['Import']['product_id'] !== #$acc[sizeof($acc)-1]['Import']['product_id']) {
$acc[] = $item;
}
return $acc;
}, []);
Demo: http://ideone.com/BP0eUJ
Update: Here is an even better linear-time algorithm that does the same as above using a fast "hash table" lookup. Again, the first-encountered product Id is kept and subsequent ones of the same Id are ignored.
$out = [];
$hashTable = [];
foreach($in as $item) {
$pid = $item['Import']['product_id'];
if(!isset($hashTable[$pid])) {
$out[] = $item;
$hashTable[$pid] = true;
}
}
Demo: http://ideone.com/5RF0og

Categories