find elements that both exist in two arrays - php

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.

Related

How can i get seperate values of array into same array?

I have a PHP array that I am trying to split into 2 different arrays into that particular array. I am trying to pull out any values for each "destination" and "balance".
Here is the array i get,
Array
(
[destination_1] => 1
[balance_1] => 1
[destination_2] => 2
[balance_2] => 2
[destination_3] => 3
[balance_3] => 3
)
I need output like,
Array (
[0] => Array (
[destination_1] => 1
[balance_1] => 1
)
[1] => Array (
[destination_2] => 2
[balance_1] => 2
)
[2] => Array (
[destination_3] => 3
[balance_3] => 3
)
)
What you need is array_chunk()
$arr = [
"destination_1" => 1,
"balance_1" => 1,
"destination_2" => 2,
"balance_2" => 2,
"destination_3" => 3,
"balance_3" => 3
];
$result = array_chunk($arr, 2, true);
Output:
Array
(
[0] => Array
(
[destination_1] => 1
[balance_1] => 1
)
[1] => Array
(
[destination_2] => 2
[balance_2] => 2
)
[2] => Array
(
[destination_3] => 3
[balance_3] => 3
)
)

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.

Get array values having same key from one array and store those values in another array

I am working on a php project. I am stuck at this point.
Here is the array that I have.
[text_numeric] => Array
(
[text] => Numeric field fillable by user
[parameters] => Array
(
[prefix] => 1
[price] => 1
[sku] =>
[quantity] =>
[weight] =>
[min_value] => 1
[max_value] => 1
)
[operand] => Array
(
[op_fix_discount] => 1
[op_fix_recharge] => 1
[op_per_unit] => 1
[op_percentage] =>
)
)
[checkbox] => Array
(
[text] => Checkbox attributes
[parameters] => Array
(
[prefix] => 1
[price] => 1
[sku] => 1
[quantity] => 1
[weight] => 1
[min_value] =>
[max_value] =>
)
[operand] => Array
(
[op_fix_discount] => 1
[op_fix_recharge] => 1
[op_per_unit] =>
[op_percentage] => 1
)
)
I want to get all the values from this array with key value "text" in another array.
Like this:
Array
(
[0]=>Numeric field fillable by user
[1]=>Checkbox attributes
)
It could have been great if you had show us your efforts to achieve that but since you are new here is the codez. You can simply get it using a foreach loop,
$new_array = array();
foreach($your_array as $k=>$row){
$new_array[$k] = $row['text'];
}
print_r($new_array);
You can also use array_map,
function getTextField($a) {
return $a['text'];
}
$texts = array_map('getTextField', $your_array);
print_r($texts);

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

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.

array_slice in multidimensional array?

I have an array in php like this :
Array
(
[0] => Array
(
[915] => 1
[1295] => 1
[1090] => 1
[1315] => 0.93759357774
[128] => 0.93759357774
[88] => 0.731522789561
[1297] => 0.731522789561
[1269] => 0.525492880722
[1298] => 0.525492880722
[121] => 0.519133966069
)
[1] => Array
(
[585] => 1
[1145] => 1
[1209] => 1
[375] => 1
[1144] => 1
[913] => 1
[1130] => 0.996351158355
[215] => 0.937096401456
[1296] => 0.879373313559
[30] => 0.866473953643
[780] => 0.866473953643
[1305] => 0.866473953643
[1293] => 0.866473953643
)
)
How do I get the 1st-5th rows of sub-array for each array, like this :
Result :
Array
(
[0] => Array
(
[915] => 1
[1295] => 1
[1090] => 1
[1315] => 0.93759357774
[128] => 0.93759357774
)
[1] => Array
(
[585] => 1
[1145] => 1
[1209] => 1
[375] => 1
[1144] => 1
)
)
$multid_array = array(/* Your Multidimensional array from above*/);
$sliced_array = array(); //setup the array you want with the sliced values.
//loop though each sub array and slice off the first 5 to a new multidimensional array
foreach ($multid_array as $sub_array) {
$sliced_array[] = array_slice($sub_array, 0, 5);
}
The $sliced_array will then contain the output you wanted.
Iterate over the array.
Read the value by reference.
Delete key-values from offset 5 till
the end. You need not collect the return value because we are using the reference to the original array.
.
foreach($mainArray as $key => &$value) {
array_splice($value,5);
}
Working ideone link
You might want to look into the php function array_splice.
http://no.php.net/manual/en/function.array-slice.php

Categories