Find a value & key in a multidimensional array [duplicate] - php

This question already has answers here:
Find value and key in multidimensional array
(8 answers)
Closed 9 years ago.
I have 2 arrays, I need to find if one of the values in array one matches one of the values in array two, a multi-dimensional array. I also need to check that the value from array one is in a specific key in array two, the "principal" key as the "authority" key may also hold this value.
here is array one:
Array
(
[0] => 17
[1] => 6
[2] => 3
[3] => 2
)
and array two [actually slightly truncated for readability]:
Array
(
[modAccessResourceGroup] => Array
(
[3] => Array
(
[0] => Array
(
[principal] => 0
[authority] => 9999
[policy] => Array
(
[load] => 1
)
)
[1] => Array
(
[principal] => 2
[authority] => 10
[policy] => Array
(
[add_children] => 1
[create] => 1
[copy] => 1
[delete] => 1
[list] => 1
[load] => 1
[move] => 1
[publish] => 1
[remove] => 1
[save] => 1
[steal_lock] => 1
[undelete] => 1
[unpublish] => 1
[view] => 1
)
)
.... truncated ....
[13] => Array
(
[principal] => 16
[authority] => 9999
[policy] => Array
(
[load] => 1
)
)
)
[8] => Array
(
[0] => Array
(
[principal] => 0
[authority] => 9999
[policy] => Array
(
[load] => 1
)
)
[1] => Array
(
[principal] => 1
[authority] => 9999
[policy] => Array
(
[add_children] => 1
[create] => 1
[copy] => 1
[delete] => 1
[list] => 1
[load] => 1
[move] => 1
[publish] => 1
[remove] => 1
[save] => 1
[steal_lock] => 1
[undelete] => 1
[unpublish] => 1
[view] => 1
)
)
[2] => Array
(
[principal] => 22
[authority] => 9999
[policy] => Array
(
[add_children] => 1
[create] => 1
[copy] => 1
[delete] => 1
[list] => 1
[load] => 1
[move] => 1
[publish] => 1
[remove] => 1
[save] => 1
[steal_lock] => 1
[undelete] => 1
[unpublish] => 1
[view] => 1
)
)
)
)
)
I was using a series of foreach(){foreach(){foreach(){}}} but it seemed very messy and inefficient. Having some trouble getting my head around this. Any ideas?

A recursive function should do the trick:
$values = array(17, 6, 3, 2, 5);
function find($array, &$values) {
foreach ($array as $key => $element) {
if (is_array($element)) {
find($element, $values);
}
elseif ($key == 'principal') {
foreach ($values as $value) {
if ($element == $value) {
echo 'Found' . PHP_EOL;
// Do stuff
}
}
}
}
}
find($array, $values);

Several things come to mind. First, in situations like this, I will usually create a separate array with just the principal values so that I can loop over the first array and just use a simple in_array() check. Secondly, if you don't want to do that, you could do something using the array_walk_recursive() function or some of the recursive examples in array_search() to go through your second array.

Related

Array_push within foreach loop overwites earlier arrey values

I'm trying to build a multidimentional array of matches found within a foreach loop. After one loop the array is correct but on the second loop, the earlier array values are overwritten. What is going on?
$matches = array();
foreach ($promotions as $promotion) {
$matches = array();
foreach ($saleitems as $saleitem) {
if ($saleitem['PROMO_CODE'] == $promotion['SALES_CODE']) {
$matches[] = array('ID'=>$saleitem['ID'], "LINENO"=>$saleitem['LINE'], "SAVING"=>"0", 'SALEINC'=>$saleitem['SALEINC']);
}
}
//other code with works out discount etc.
$linesarray[] = array("CODE"=>$promotion['CODE'], "LINES"=>$matches);
print_r($linesarray);
echo "<p>";
}
Outputs this:
Array ( [0] => Array ( [CODE] => 5 [LINES] => Array ( [0] => Array ([ID] => 51016 [LINENO] => 4 [SAVING] => 5 [SALEINC] => 15.00 ) [1] => Array ([ID] => 51013 [LINENO] => 3 [SAVING] => 5 [SALEINC] => 15.00 ) ) ) )
Array ( [0] => Array ( [CODE] => 5 [LINES] => Array ( [0] => Array ( [ID] => 51016 [LINENO] => 4 [SAVING] => 5 [SALEINC] => 15.00 ) [1] => Array ([ID] => 43930 [LINENO] => 2 [SAVING] => 0 [SALEINC] => 16.00 ) ) ) [1] => Array ( [CODE] => 7 [LINES] => Array ( [0] => Array ([ID] => 43914 [LINENO] => 1 [SAVING] => 6 [SALEINC] => 16.00 ) [1] => Array ([ID] => 43930 [LINENO] => 2 [SAVING] => 6 [SALEINC] => 16.00 ) ) ) )
As you can see LINENO 3 has been replaced the first array on second loop. Why?
The $matches = array(); inside the loop will reinitialize the array. The $matches = array(); before the loop is fine.
$matches[] = array('ID'=>$saleitem['ID'], "LINENO"=>$saleitem['LINE'], "SAVING"=>"0", 'SALEINC'=>$saleitem['SALEINC']);
I see you fixed "SAVING"=>"0" but your outputs have other result [SAVING] => 5 Did you run your code again?
You should give us $promotions and $saleitems array.

Filter a multidimensional array - filter_array (PHP)

How do you filter this multidimensional array with array_filter() based on [channel]?
Array
(
[0] => Array
(
[268a9d2d25fc2b9765c7cd7b8a768d3e] => Array
(
[dj_name] => Emilian
[show_name] => TechnoShow
[channel] => techno
[show_image] => http://www.digitalark.ro/dieselfm/wp-content/uploads/2016/01/avatar.jpg
[time] => 0
[time_end] => 1
[sun1] => 1
[sun2] => 1
[sun3] => 1
[sun4] => 1
[sun5] => 1
)
)
[1] => Array
(
[e13268de7c56db42f8aeab2ab4c607f2] => Array
(
[dj_name] => John Doe
[show_name] => John Doe`s Trance Show
[channel] => trance
[show_image] => http://www.digitalark.ro/dieselfm/wp-content/uploads/2016/01/dummy.jpg
[time] => 11
[time_end] => 11
[mon1] => 1
[mon2] => 1
[tue2] => 1
[mon3] => 1
[fri3] => 1
[mon4] => 1
[mon5] => 1
)
)
)
Results should have only the arrays that have the value "techno", for example:
Array
(
[0] => Array
(
[268a9d2d25fc2b9765c7cd7b8a768d3e] => Array
(
[dj_name] => Emilian
[show_name] => TechnoShow
[channel] => techno
[show_image] => http://www.digitalark.ro/dieselfm/wp-content/uploads/2016/01/avatar.jpg
[time] => 0
[time_end] => 1
[sun1] => 1
[sun2] => 1
[sun3] => 1
[sun4] => 1
[sun5] => 1
)
))
I have tried using:
$data = array_filter($dataraw, function($fs) use ($genre) {return $fs['channel'] == $genre});
EDIT:
$djs = get_posts($args);
foreach ($djs as $dj) {
$temp = maybe_unserialize(get_post_meta($dj->ID, 'show_data',true));
if ($temp) $show_data[] = maybe_unserialize(get_post_meta($dj->ID, 'show_data',true));
}
$datax = array_filter($show_data, function($fs) use ($genre) {
return array_values($fs)[0]['channel'] == $genre;
});
print_r($datax);
I'll assume the parse error in your code was a copy/paste mistake. The problem is that the element passed into the function is a deeper array. Try:
return current($fs)['channel'] === $genre;
Also you might want to use === so the results are as expected.

find elements that both exist in two arrays

I have two arrays like the following:
Array1
Array ( [price] => 117.00 [recurring_profile] => [use_config_gift_message_available] => 1 [stock_data] => Array ( [use_config_manage_stock] => 1 [original_inventory_qty] => 100 [qty] => 100 [use_config_min_qty] => 1 [use_config_min_sale_qty] => 1 [use_config_max_sale_qty] => 1 [is_qty_decimal] => 0 [is_decimal_divided] => 0 [use_config_backorders] => 1 [use_config_notify_stock_qty] => 1 [use_config_enable_qty_increments] => 1 [use_config_qty_increments] => 1 [is_in_stock] => 1 ) [website_ids] => Array ( [0] => 1 ) [can_save_configurable_attributes] => [can_save_custom_options] => [can_save_bundle_selections] => [type_has_options] => [type_has_required_options] => )
Array2
Array ( [price] => 118.0000 )
I use
$newarr =array_intersect_assoc($oldValues, $newValues);
but $newarr will be blank, any ideas?
My expected results:
$newArray1 = Array ( [price] => 117.00 );
It's blank, because price has different values 117.00 and 118.0000
You can try array_intersect_key
array_intersect_assoc() will only return items where BOTH the key and value match.

SUM values across two multidimensional arrays in PHP

I have a problem how to SUM values of two multidimensional arrays in PHP.
Two arrays are storred in variables for example $array_1 and $array_2
Both $array_1 and $array_2 have sub-arrays, which again have data stored in their respective arrays. Notice that data is organized as x-data and y-data, where y-data is just a DATE stamp.
I need to SUM x-data values of these two arrays $array_1 and $array_2, with respect to y-data (DATE) dimension. This is why it got tricky for me, the closes answers I have found have only numbers.
Example:
$array_1
Array
(
[0] => object
(
[x-data] => Array
(
[data1] => 0
[data2] => 1
[data3] => 2
[data4] => 3
[data5] => 4
[data6] => 5
[data7] => 6
[data8] => 7
)
[y-data] => Array
(
[date] => 20141127
)
)
[1] => object
(
[x-data] => Array
(
[data1] => 2
[data2] => 4
[data3] => 6
[data4] => 8
[data5] => 10
[data6] => 12
[data7] => 14
[data8] => 16
)
[y-data] => Array
(
[date] => 20141128
)
)
)
$array_2
Array
(
[0] => object
(
[x-data] => Array
(
[data1] => 0
[data2] => 1
[data3] => 2
[data4] => 3
[data5] => 4
[data6] => 5
[data7] => 6
[data8] => 7
)
[y-data] => Array
(
[date] => 20141127
)
)
[1] => object
(
[x-data] => Array
(
[data1] => 0
[data2] => 1
[data3] => 2
[data4] => 3
[data5] => 4
[data6] => 5
[data7] => 6
[data8] => 7
)
[y-data] => Array
(
[date] => 20141128
)
)
)
$result = $array_1 + $array_2 should look like this:
Array
(
[0] => object
(
[x-data] => Array
(
[data1] => 0
[data2] => 2
[data3] => 4
[data4] => 6
[data5] => 8
[data6] => 10
[data7] => 12
[data8] => 14
)
[y-data] => Array
(
[date] => 20141127
)
)
[1] => object
(
[x-data] => Array
(
[data1] => 2
[data2] => 5
[data3] => 8
[data4] => 11
[data5] => 14
[data6] => 17
[data7] => 20
[data8] => 23
)
[y-data] => Array
(
[date] => 20141128
)
)
)
I have tried some things, like foreach() inside foreach() but I got a multiplication effect of the array values (2 x 2 = 4 subarrays instead of 2).
Any help?
Thanks!
Here's a partial answer as this should only only work if $array1 and $array2 have same number of elements and have unique y-datas, and each of their x-data (with y-data in common) have the same number of elements.
$result = array_multisum($array1, $array2);
function array_multisum($array1, $array2)
{
$newArray = array();
foreach ($array1 as $object1) {
$newObject = new stdClass();
// find object in the 2nd array having the same data as current object
foreach ($array2 as $object2) {
// if object is found, sum the x-data with the current object
if ($object1->{'y-data'}['date'] === $object2->{'y-data'}['date']) {
$newObject->{'x-data'} = data_sum($object1->{'x-data'}, $object2->{'x-data'});
break;
}
}
$newObject->{'y-data'} = $object1->{'y-data'};
$newArray[] = $newObject;
}
return $newArray;
}
function data_sum($data1, $data2)
{
$newData = array();
// sum up the values for each key
foreach (array_keys($data1) as $key) {
$newData[$key] = $data1[$key] + $data2[$key];
}
return $newData;
}

Get particular data with multiple index value with/without loop in cakephp

I have an array in which i want only lineNo and Isdirty field in each array .
My demo code is
Array
(
[CodeConfiguration] => Array
(
[0] => Array
(
[ObjectType] => 12
[LineNo] => 1
[CompanyID] => 1
[BranchID] => 46
[ModifiedDate] => 2014-04-25 05:10:15
[RevisionNumber] => 6
[IsDirty] =>
)
)
[TaxConfiguration] => Array
(
[0] => Array
(
[LineNo] => 2
[IsDirty] => 1
[ItemGroupID] =>
[TaxID] =>
[CalculationType_080] => 430
[RevisionNumber] => 1
)
[1] => Array
(
[LineNo] => 1
[IsDirty] => 1
[ItemGroupID] =>
[TaxID] =>
[CalculationType_080] => 372
[RevisionNumber] => 1
)
)
)
Only LineNo And Isdirty field want in every index array .So please suggest me solution.
You can sue following;
$finalArr = array();
foreach ($arr as $key => $item) {
foreach ($item as $k => $v) {
$finalArr[$key][] = array(
"LineNo" => $v["LineNo"],
"IsDirty" => $v["IsDirty"]
);
}
}
Here is a working demo: Demo

Categories