PHP/Laravel Array diff on nested arrays - remove false values - php

I have two arrays that contain nested arrays also, where I want to compare them against each other. If the value in the first array is false, I want that key removed from the 2nd array.
1st array: $arrayOne:
'entities' => [
'number' => false,
'items' => [
[
'name' => false,
],
],
'totals' => [
'gross' => true,
'tax' => [
'amount' => true,
],
]
];
Then, I have the second array $arrayTwo:
'entities' => [
'number' => '8000000',
'items' => [
[
'name' => 'Bolt cutter',
],
[
'name' => 'Wire cutter',
],
],
'totals' => [
'gross' => 120.52,
'tax' => [
'amount' => 90.21,
],
]
];
As you can see in the first array, the keys: number and items.name is false. These two keys should not be in the final array, thus making it look like this:
'entities' => [
'items' => [],
'totals' => [
'gross' => 120.52,
'tax' => [],
]
];
How can I accomplish this with nested arrays? I have managed to do it for the "1st level" of the array using below:
foreach ($arrayOne as $key => $entity) {
if ($entity === false) {
unset($arrayTwo[$key]);
}
}
However, as said, this only handles the first level of the array and not the nested array(s).

i did test this, and it will work fine.
use Illuminate\Support\Arr;
$flattened_array1 = Arr::dot($array1);
$flattened_array2 = Arr::dot($array2);
foreach ($flattened_array1 as $key => $entity) {
if ($entity === false) {
// remove all number key from first array
$filteredKey = preg_replace('/[0-9]+\./', '', $key);
foreach ($flattened_array2 as $key2 => $value2) {
if(preg_replace('/[0-9]+\./', '', $key2) == $filteredKey){
// comparing to removed number key version of second array
unset($flattened_array2[$key2]);
}
}
}
}
// undot the array
$final_array = [];
foreach ($flattened_array2 as $key => $value) {
array_set($final_array , $key, $value);
}
new Update to support empty array when nested array
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
$flattened_array1 = Arr::dot($array1);
$flattened_array2 = Arr::dot($array2);
// Function to check string starting
foreach ($flattened_array1 as $key => $entity) {
if ($entity === false) {
// remove all number key from first array
$removerCounter[$key] = 0;
$filteredKey = preg_replace('/[0-9]+\./', '', $key);
foreach ($flattened_array2 as $key2 => $value2) {
// comparing to removed number key version of second array
if(preg_replace('/[0-9]+\./', '', $key2) == $filteredKey){
// check if it contain filtered number key then
if(preg_replace('/[0-9]+\./', '', $key2) != $key2){
$currentKey = $key2;
preg_match_all("/[0-9]+\./", $currentKey, $hits);
if(!isset($hits[0]) || empty($hits[0]) ){
break;
}
$needle = $hits[0] [ count($hits[0]) - 1 ];
// it is inside key not at the begining
if(stripos($currentKey, $needle) != 0){
// if parent key has no child so let it be empty array
$parentKey = trim(substr($currentKey, 0, stripos($currentKey, $needle)), '.');
// search inside secound array for parentKey of this nested array
$searchAndFound = false;
foreach ($flattened_array2 as $inner_key2 => $inner_value2) {
if( !($searchAndFound) && $inner_key2 != $key2 && Str::startsWith($inner_key2, $parentKey) ) {
$searchAndFound = true;
}
}
if( $searchAndFound ){
// die("e");
unset($flattened_array2[$key2]);
$removerCounter[$key]++;
//remove this key and go for next key in secound array
continue;
}
if($removerCounter[$key] > 0){
unset($flattened_array2[$key2]);
}
$flattened_array2[$parentKey] = [];
}
}
else{
unset($flattened_array2[$key2]);
}
}
}
}
}
// undot the array
$final_array = [];
foreach ($flattened_array2 as $key => $value) {
array_set($final_array , $key, $value);
}
it is more complated that it expected to be . but does the trick :s.

Related

How to generate array path for specific key by looping through dynamic multidimensional array?

I have a dynamic multidimensional array as below:
$cityList = [
'AUS' => [
'VIC' => [
'population' => [ 'total' => '5M']
'Richmond' => [
'population' => [ 'total' => '0.15M']
]
],
'NSW' => [
'Carlton' => [
'population' => [ 'total' => '8M']
]
]
]
];
Here, the column population may or may not be present on all dimension. However, if it's present then it'll always have total as sub array as above.
Now, I need to traverse through the array and generate all path to population if exist.
I've written code as below:
public function fetchAllPopulation(array $cityList, $path = '', &$cityWithPopulation)
{
foreach ($cityList as $key => $city) {
if (is_array($city) && $key != 'population') {
$path .= $path == '' ? $key: "##$key";
$this->fetchAllPopulation($city, $path, $cityWithPopulation);
} else {
$population = $city['total'];
$cityWithPopulation[$path] = $population;
}
}
return $assetWithPathsAndIds;
}
Expected output:
[
'AUS##VIC' => '5M',
'AUS##VIC##Richmond' => '0.15M',
'AUS##NSW##Carlton' => '8M'
]
Actual output:
[
'AUS##VIC' => '5M',
'AUS##VIC##Richmond' => '0.15M',
'AUS##VIC##NSW##Carlton' => '8M' // this is incorrect
]
The problem is if any column has more than 2 dimensions, then the previous key will be appended on the next one as above.
Any feedback or correction to my code will be appreciated. Thanks!
This way:
public function fetchAllPopulation(array $cityList, $path, &$cityWithPopulation)
{
foreach ($cityList as $key => $city) {
if (is_array($city) && $key != 'population') {
$subPath = $path . ($path == '' ? $key: "##$key");
$this->fetchAllPopulation($city, $subPath, $cityWithPopulation);
} else {
$population = $city['total'];
$cityWithPopulation[$path] = $population;
}
}
return $assetWithPathsAndIds;
}

Filter array with array_walk_recursive but deny specific values

I am trying to filter an array
$array = [
[
'id' => 1,
'some_key' => 'some_value',
'attribute' => [
'id' => 45,
'cat_id' => 1
],
'sub' => [
'id' => 17,
'some_key' => 'some_value',
'attribute' => [
'id' => 47,
'cat_id' => 17
],
],
],
[
'id' => 2,
'some_key' => 'some_value',
'sub' => [
'id' => 19,
'some_key' => 'some_value',
],
]
];
$childArray = [];
array_walk_recursive($array, static function($value, $key) use(&$childArray){
if($key === 'id') {
$childArray[] = $value;
}
});
This returns me an array of all array-fields having id as key.
[1,45,17,47,2,19]
But there is a small problem, some of the array have an key called attribute containing an idkey field that I dont want to have.
[1,17,2,19] //this is what I want
Is there a way to say "don't take the id inside attribute" ?
My current solution, I added a filter before my filter :D
/**
* #param array $array
* #param string $unwanted_key
*/
private function recursive_unset(&$array, $unwanted_key)
{
unset($array[$unwanted_key]);
foreach ($array as &$value) {
if (is_array($value)) {
$this->recursive_unset($value, $unwanted_key);
}
}
}
but this seems like this is not the best practice ^^
You can traverse recursively manually instead of array_walk_recursive and avoid all under attribute key.
<?php
$childArray = [];
function collectIDs($arr,&$childArray){
foreach($arr as $key => $value){
if($key === 'attribute') continue;
if(is_array($value)) collectIDs($value,$childArray);
else if($key === 'id') $childArray[] = $value;
}
}
collectIDs($array,$childArray);
print_r($childArray);
Demo: https://3v4l.org/V6uFf
Find a function that will flatten your array. The result should look like this (I have a class for this):
array (
0 =>
array (
'id' => 1,
'some_key' => "some_value",
'attribute.id' => 45,
'attribute.cat_id' => 1,
'sub.id' => 17,
'sub.some_key' => "some_value",
'sub.attribute.id' => 47,
'sub.attribute.cat_id' => 17,
),
1 =>
array (
'id' => 2,
'some_key' => "some_value",
'sub.id' => 19,
'sub.some_key' => "some_value",
),
)
So you have all keys available and can work with a modified array_walk.
$childArray = [];
array_walk_recursive($data, static function($value, $key) use(&$childArray){
$keys = array_reverse(explode(".",$key));
if($keys[0] === 'id' AND (!isset($keys[1]) OR $keys[1] != 'attribute')) {
$childArray[] = $value;
}
});
The RecursiveArrayIterator class is also very suitable when array values ​​are to be collected depending on keys and values ​​on different levels.
$result = [];
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($it as $key => $value) {
$parentLevel = $it->getDepth()-1;
$parentKey = $it->getSubIterator($parentLevel)->key();
if($key === 'id' AND $parentKey !== 'attribute'){
$result[] = $value;
}
}
var_export($result);
//array ( 0 => 1, 1 => 17, 2 => 2, 3 => 19, )
To return the qualifying ids instead of creating a reference variable, merge recursive calls of the function as you iterate.
Code: (Demo)
function getIds($array) {
$ids = [];
foreach ($array as $key => $value) {
if ($key === 'id') {
$ids[] = $value;
} elseif ($key !== 'attribute' && is_array($value)) {
$ids = array_merge($ids, getIds($value));
}
}
return $ids;
}
var_export(getIds($array));
Output:
array (
0 => 1,
1 => 17,
2 => 2,
3 => 19,
)

PHP remove array if subarray empty

my array image just like this, if subarray "name" is empty or null i want delete array, how to do that ?
here my current script
$data = array();
$fixedData = array();
$countyName = array();
$numrow = 2;
echo "<pre>";
// insert to tb participant => 1
foreach($sheet as $key => $row){
$data[] = array(
'name' => $this->split_name($row['B']),
'phone' => $row['D'],
'mobile' => $row['E'],
'institution' => $row['F'],
'departement' => $row['G'],
'address' => $row['H'],
'country' => $row['I'],
);
$numrow++;
}
unset($data[0]); //delete first row
$data = array_values($data);
//loop search data
var_dump ($data);
die();
Assume that you have the following data set,
$array = [
[
'name' => 'not null', 'phone' => 12546
],[
'name' => '', 'phone' => 852147
],[
'name' => null, 'phone' => 96325874
],[
'name' => 'have value', 'phone' => 12546
],
];
You can filter the nulled or empty values like several ways :
1-
foreach ($array as $key => &$value) {
if (empty($value['name']) || is_null($value['name'])) {
$value = null;
}
}
$array = array_filter($array);
2-
$newData = [];
foreach ($array as $key => $value) {
if (!empty($value['name']) && !is_null($value['name'])) {
$newData[] = $value;
}
}
3- using array_walk
$newData = [];
array_walk($array, function ($value, $key) use (&$newData) {
if (!empty($value['name']) && !is_null($value['name'])) {
$newData[] = $value;
}
});
4- using array_filter
$newData = array_filter($array, function ($value) {
if (!empty($value['name']) && !is_null($value['name'])) {
return $value;
}
});
<?php
$data = array();
$fixedData = array();
$countyName = array();
$numrow = 2;
echo "<pre>";
// insert to tb participant => 1
foreach($sheet as $key => $row){
if($this->split_name($row['B'])!=='' && $this->split_name($row['B'])!==NULL){
$data[] = array(
'name' => $this->split_name($row['B']),
'phone' => $row['D'],
'mobile' => $row['E'],
'institution' => $row['F'],
'departement' => $row['G'],
'address' => $row['H'],
'country' => $row['I'],
);
$numrow++;
}
}
//loop search data
var_dump ($data);
die();
I simple put an if condition inside your loop so you can check if your value is null or empty and if it is then you don't fill your new array. Also moved your counter inside the if so you increment it only in a success array push
A more "elegant" way for your if condition is this as well:
if (!empty($this->split_name($row['B'])) && !is_null($this->split_name($row['B'])))

Matching multidimensional array value with two values from other array in php

I have array name $main_array
$main_array = [
[
'product_id' => '1',
'values' => '1"'
],
[
'product_id' => '4',
'values' => '1"'
],
[
'product_id' => '4',
'values' => 'blue'
],
[
'product_id' => '5',
'values' => 'blue'
]
];
I want to check values from other array
$check_array = [
'1"','blue'
];
Find product_id where 1" && blue both matching
Expected output ::
$output = [
[
'product_id' => '4',
'values' => '1"'
],
[
'product_id' => '4',
'values' => 'blue'
]
];
You could use an array to store matching elements using the product id as key.
$include = [] ;
foreach ($main_array as $key => $item) {
// if values match to $check_array
if (in_array($item['values'], $check_array)) {
// store using product id as key
$pid = $item['product_id'] ;
$include[$pid][] = $key;
}
}
// Filter to keep only items that match with all conditions
$include = array_filter($include, function($a) use ($check_array) {
return count($a) == count($check_array) ;
}) ;
$include = reset($include) ; // Get the first
// Recreate final array :
$out = [] ;
foreach ($include as $elem) {
$out[] = $main_array[$elem] ;
}
print_r($out);
Will outputs :
Array
(
[0] => Array
(
[product_id] => 4
[values] => 1"
)
[1] => Array
(
[product_id] => 4
[values] => blue
)
)
You may use nested foreach cycles. Supposed that $check_array containes two fields, you can write a code like this:
$output = array();
$match_array = array();
//check the first field correspondence
foreach ($main_array as $key1=>$sub_main) {
foreach ($sub_main as $key2=>$item) {
if ($check_array[0] == $item['values']) {
$match_array[] = $item['product_id'];
}
}
}
//try to match the second field
foreach ($main_array as $key1=>$sub_main) {
foreach ($sub_main as $key2=>$item) {
if ($check_array[1] == $item['values']) {
if (in_array($item['product_id'], $match_array) {
$output[] = array($item['product_id'], $check_array[1]);
$output[] = array($item['product_id'], $check_array[2]);
}
}
}
}

PHP - How to remove empty entries of an array recursively?

I need to remove empty entries on multilevel arrays. For now I can remove entries with empty sub-arrays, but not empty arrays... confused, so do I... I think the code will help to explain better...
<?php
/**
*
* This function remove empty entries on arrays
* #param array $array
*/
function removeEmptysFromArray($array) {
$filtered = array_filter($array, 'removeEmptyItems');
return $filtered;
}
/**
*
* This is a Callback function to use in array_filter()
* #param array $item
*/
function removeEmptyItems($item) {
if (is_array($item)) {
return array_filter($item, 'removeEmptyItems');
}
if (!empty($item)) {
return true;
}
}
$raw = array(
'firstname' => 'Foo',
'lastname' => 'Bar',
'nickname' => '',
'birthdate' => array(
'day' => '',
'month' => '',
'year' => '',
),
'likes' => array(
'cars' => array('Subaru Impreza WRX STi', 'Mitsubishi Evo', 'Nissan GTR'),
'bikes' => array(),
),
);
print_r(removeEmptysFromArray($raw));
?>
Ok, this code will remove "nickname", "birthdate" but is not removing "bikes" that have an empty array.
My question is... How to remove the "bikes" entry?
Best Regards,
Sorry for my english...
Try this code:
<?php
function array_remove_empty($haystack)
{
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$haystack[$key] = array_remove_empty($haystack[$key]);
}
if (empty($haystack[$key])) {
unset($haystack[$key]);
}
}
return $haystack;
}
$raw = array(
'firstname' => 'Foo',
'lastname' => 'Bar',
'nickname' => '',
'birthdate' => array(
'day' => '',
'month' => '',
'year' => '',
),
'likes' => array(
'cars' => array('Subaru Impreza WRX STi', 'Mitsubishi Evo', 'Nissan GTR'),
'bikes' => array(),
),
);
print_r(array_remove_empty($raw));
array_filter(explode('/', '/home/teste sdsd/ /'), 'trim');
//Result
[
1 => "home",
2 => "teste sdsd",
]
//-----------
array_filter(explode('/', '/home/teste sdsd/ /'), 'strlen')
//Result
[
1 => "home",
2 => "teste sdsd",
3 => " ",
]
I think this should solve your problem.
$retArray =array_filter($array, arrayFilter);
function arrayFilter($array) {
if(!empty($array)) {
return array_filter($array);
}
}
Here is my solution, it will remove exactly specified list of empty values recursively:
/**
* Remove elements from array by exact values list recursively
*
* #param array $haystack
* #param array $values
*
* #return array
*/
function array_remove_by_values(array $haystack, array $values)
{
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$haystack[$key] = array_remove_by_values($haystack[$key], $values);
}
if (in_array($haystack[$key], $values, true)) {
unset($haystack[$key]);
}
}
return $haystack;
}
You can use it like this:
$clean = array_remove_by_values($raw, ['', null, []]);
Note, it removes empty sub arrays if you pass [] as one of values.
Recursively clear multidimensional array of empty'ish items:
final class ArrayCleaner
{
public static function clean(array $value): array
{
foreach ($value as $k => $v) {
if (\is_array($v)) {
$value[$k] = self::clean($v);
if (0 == \count($value[$k])) {
unset($value[$k]);
}
} elseif (empty($v)) {
unset($value[$k]);
}
}
return $value;
}
}
Unit test:
final class ArrayCleanerTest
{
public function testItCleans(): void
{
$input = [
'empty_string_to_remove' => '',
'empty_int_to_remove' => 0,
'empty_string_number_to_remove' => '0',
'value_to_keep' => 5,
'empty_array_to_remove' => [],
'empty_array_of_empty_arrays_to_remove' => [
'one' => [],
'two' => [],
'three' => [false, null, '0'],
],
'array_to_keep_1' => [
'value' => 1,
],
'array_to_keep_2' => [
'empty_to_remove' => [],
'array_to_keep' => ['yes'],
],
];
$expected = [
'value_to_keep' => 5,
'array_to_keep_1' => [
'value' => 1,
],
'array_to_keep_2' => [
'array_to_keep' => ['yes'],
],
];
$this->assertEquals($expected, ArrayCleaner::clean($input));
}
}
Working proof of concept at 3v4l.org
My function:
function removeEmptyItems($item)
{
if (is_array($item)) {
$item = array_filter($item, 'removeEmptyItems');
}
return !empty($item);
}
$nonEmpty = array_filter($raw, 'removeEmptyItems');
If you want array_filter to work recursively, you'll need to make sure that the subsequent calls may edit the deeper nested items of the array. Short: You'll need to pass it by reference:
function removeEmptyItems(&$item) {
if (is_array($item) && $item) {
$item = array_filter(&$item, 'removeEmptyItems');
}
return !!$item;
}

Categories