extract values from multidemensional array - php

I have two arrays that are structured like this
$array1 = Array
(
[0] => Array
(
['story_id'] => 47789
)
[1] => Array
(
['story_id'] => 47779
)
[2] => Array
(
['story_id'] => 47776
)
[3] => Array
(
['story_id'] => 47773
)
[4] => Array
(
['story_id'] => 47763
)
)
$array2 = Array
(
[0] => Array
(
['story_id'] => 47789
)
[1] => Array
(
['story_id'] => 47777
)
[2] => Array
(
['story_id'] => 47776
)
[3] => Array
(
['story_id'] => 47773
)
[4] => Array
(
['story_id'] => 47763
)
)
and I want to get the difference of array1 from array2 so I tried using
$results = array_diff($array1, $array2);
but it turns up empty is there any easy way around this or would it be best for me to get the arrays boiled down and if so is there easy way to do that ?

It because that the array_diff is only use for 1 dimension array. For your 2 array, let use some code from php.net
function multidimensional_array_diff($a1, $a2)
{
$r = array();
foreach ($a2 as $key => $second) {
foreach ($a1 as $key => $first) {
if (isset($a2[$key])) {
foreach ($first as $first_value) {
foreach ($second as $second_value) {
if ($first_value == $second_value) {
$true = true;
break;
}
}
if (!isset($true)) {
$r[$key][] = $first_value;
}
unset($true);
}
} else {
$r[$key] = $first;
}
}
}
return $r;
}

Related

How to make array key from array data value in associative array

I have a multidimensional associative array which has a set of array. I want to change my array index value from some array value.
I already tried some array functions but my array also contains some null array so laravel function keyBy not give me wanted result.
$arr1=array(0 =>array(),1=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
2 =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
My expected result array must be like this
$arr2=array(0 =>array(),'baroque'=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
'adidas' =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
You can use the classic foreach. Check if the handle on element 0 exists using isset, if it does, use that as the key.
$arr1 = //...
$result = array();
foreach($arr1 as $key => $val) {
if (is_array($val) && isset($val[0]["handle"])) $result[ $val[0]["handle"] ] = $val;
else $result[$key] = $val;
}
$result will be:
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
You can use without condition by grouping at the handle as key directly.
$result = [];
foreach ($arr as $key => $value) {
if (!empty($value)) {
foreach ($value as $key1 => $value1) {
$result[$value1['handle']][] = $value1;
}
} else {
$result[] = $value;
}
}
Demo
Output:-
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
Try this..
$res = [];
foreach($x as $key => $value)
{
if(empty($value))
{
$res[] = $value;
}
else
{
foreach($value as $v => $k)
{
if(array_key_exists($k['handle'],$res))
{
$res[$k['handle']][] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
else
{
$res[$k['handle']][0] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
}
}
}
The result is going to be like this.
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)

Sort multidimensional array by value unique array keys

I want to sort Multi dimensional array based up on the value, please check following array,
Array
(
[1] => Array
(
[70000] => Aceh
)
[2] => Array
(
[70024] => Sumatera Utara
)
[3] => Array
(
[70058] => Barat
)
[4] => Array
(
[70078] => Riau
)
[5] => Array
(
[70091] => Jambi
)
)
I want it to be like this after sort, please check below array.
Array
(
[1] => Array
(
[70000] => Aceh
)
[2] => Array
(
[70024] => Barat
)
[3] => Array
(
[70058] => Jambi
)
[4] => Array
(
[70078] => Riau
)
[5] => Array
(
[70091] => Sumatera Utara
)
)
Can any one help me with the good solution please. Thank you!.
Very strange output that you're after. Use something like this:
function weirdSort($array) {
$out = [];
$keys = [];
$values = [];
foreach($array as $k => $v) {
$values[] = $v;
$keys[] = $k;
}
usort($values);
foreach($keys as $i => $key) {
$out[] = [$key => $values[$i];
}
return $out;
}
I don't know this way is good or bad but I got wanted output by usort
function cmp($a, $b) {
if ($a[key($a)] == $b[key($b)]) return 0;
return ($a[key($a)] > $b[key($b)]) ? 1 : -1;
}
usort( $levelOneArray, 'cmp' );
print_r($levelOneArray);

Multidimensional array sorting by value in php

I have this multidimensional result array in php. I want to sort this array by the values of [name] without using foreach loop. Plz help me.
Array has to sory by [name]. Thanks in advance.
Array
(
[result] => Array
(
[projects] => Array
(
[0] => Array
(
[name] => Project-3
[releases] => Array
(
[0] => Array
(
[id] => 752676125
)
)
)
[1] => Array
(
[name] => Project-1
[releases] => Array
(
[0] => Array
(
[id] => 752676126
)
)
)
[2] => Array
(
[name] => Project-2
[releases] => Array
(
[0] => Array
(
[id] => 752676127
)
)
)
)
)
)
First of all extract $mult_arry['result']['projects'] and run the sorting function as follows.
<?php
function msort($array, $key, $sort_flags = SORT_REGULAR) {
if (is_array($array) && count($array) > 0) {
if (!empty($key)) {
$mapping = array();
foreach ($array as $k => $v) {
$sort_key = '';
if (!is_array($key)) {
$sort_key = $v[$key];
} else {
// #TODO This should be fixed, now it will be sorted as string
foreach ($key as $key_key) {
$sort_key .= $v[$key_key];
}
$sort_flags = SORT_STRING;
}
$mapping[$k] = $sort_key;
}
asort($mapping, $sort_flags);
$sorted = array();
foreach ($mapping as $k => $v) {
$sorted[] = $array[$k];
}
return $sorted;
}
}
return $array;
}
$mult_arry=array('result'=>array('projects'=>array(
array('name'=>'Project-3','releases'=>array(array('id' => 752676125))),
array('name'=>'Project-1','releases'=>array(array('id' => 752676126))),
array('name'=>'Project-2','releases'=>array(array('id' => 752676127)))
)));
$mult_arry_extracted=$mult_arry['result']['projects'];
echo "<pre>";
print_r($mult_arry_extracted);
$mult_arry_sorted_byname = msort($mult_arry_extracted, array('name'));
print_r($mult_arry_sorted_byname);
echo "</pre>";
?>
More information here

How to sum array values based on keys?

The first array
Array
(
[0] => Array
(
[1] => 2
)
[1] => Array
(
[1] => 2
)
[2] => Array
(
[2] => 1
)
[3] => Array
(
[3] => 1
)
)
I want output like
Array
(
[0] => Array
(
[1] => 4
)
[1] => Array
(
[2] => 1
)
[2] => Array
(
[3] => 1
)
)
How can i do this?
Seems like a good case for array_reduce():
$res = array_chunk(array_reduce($arr, function(&$current, $item) {
foreach ($item as $key => $value) {
if (!isset($current[$key])) {
$current[$key] = 0;
}
$current[$key] += $value;
}
return $current;
}, []), 1, true);
For the final result I'm using array_chunk(); it takes an array and creates single element sub arrays of each element.
$result = array();
foreach ($input as $subarray) {
foreach ($subarray as $key => $value) {
if (isset($result[$key])) {
$result[$key][$key] += $value;
} else {
$result[$key] = array($key => $value);
}
}
}
$result = array_values($result); // Convert from associative array to indexed array

Recursive PHP function that copies multidimensional array but replaces empty values

I have a multidimensional array that could be any size or depth. I'm basically trying replace empty values with a value but only in certain cases. Here is an example of the array, it's quite large but I want to illustrate my point well:
[field_ter] =>
[field_title] => Array
(
[0] => Array
(
[value] =>
)
)
[field_firstnames] => Array
(
[0] => Array
(
[value] => test9
)
)
[field_birth] => Array
(
[0] => Array
(
[value] =>
)
)
[field_postal] => Array
(
[0] => Array
(
[value] =>
)
)
[group_certificates] => Array
(
[0] => Array
(
[_delta] => 0
[field_cert_details] => Array
(
[value] =>
)
[field_cert_issuedate] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_expiry] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_issue_country] => Array
(
[value] =>
)
[field_cert_limits] => Array
(
[value] =>
)
)
[1] => Array
(
[_delta] => 1
[field_cert_details] => Array
(
[value] =>
)
[field_cert_issuedate] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_expiry] => Array
(
[value] => Array
(
[date] =>
)
)
[field_cert_issue_country] => Array
(
[value] =>
)
[field_cert_limits] => Array
(
[value] =>
)
)
)
What I'm trying to do is find any element in the array that is empty, then replace the empty value with a value. I have an array of exceptions where the empty element is not replaced. This is the function I'm working on at the moment:
function check_empty(&$array) {
$exceptions = array('changed', 'form_build_id','date', 'status', 'op');
// This is the array we will return at the end of the function
$new_array = array();
foreach($array as $key => $value) {
if(is_array($value)) {
check_empty($value);
}
elseif ($value == '' && !in_array($key, $exceptions)) {
$new_array[$key] = '$$$';
}
else {
$new_array[$key] = $value;
}
}
return $new_array;
}
Could someone help me tweak my function or point me in the direction of a better way? I tried array_walk_recursive but it doesn't work with my arrays.
You need to assign the return value of the recursive check_empty:
function check_empty($array) {
$exceptions = array('changed', 'form_build_id','date', 'status', 'op');
// This is the array we will return at the end of the function
$new_array = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$new_array[$key] = check_empty($value);
}
elseif ($value == '' && !in_array($key, $exceptions)) {
$new_array[$key] = '$$$';
}
else {
$new_array[$key] = $value;
}
}
return $new_array;
}
if(is_array($value)) {
check_empty($value);
}
Here you don't return resulting value to anywhere. Probably that's the problem

Categories