I have a array in below format
Array ([abc] => Array ( [0] => Array ( [abc_id] => 10 [status] => true ) [1] =>
Array ( [abc_id] => 11 [status] => true ) [2] => Array ( [abc_id] => 12 [status] => true ) )
[pqr] => Array ( ) [xyz] => Array ( [0] => Array ( [xyz_id] => 8 [status] => false )
[1] => Array ( [xyz_id] => 9 [status] => false ) ) [mno] => Array ( ) [def] => Array ( ) [unit_id] => 1)
And I want to check if there is a status = false word in the entire array.
I tried using array_in but couldn't succeed. Can anyone give me some proper solution for the above issue? Is there any other way to check if the array includes false anywhere.
You can use array_walk_recursive() which will go through all levels of a multidimensional array - BUT only the leaf nodes. Although that is fine as that is what you want.
This code has a found flag and a simple function which checks the label and the value for the values your after and sets the flag to be true if they both match...
$found = false;
array_walk_recursive($aray, function ($item, $key) use (&$found) {
if ( $key === "status" && $item == false) {
$found = true;
}
});
echo $found;
Related
I have a problem to understand why this doesn't work. I have recursive function who check if value in multidimensional array exists.
Then I have get_teams() function from which I want to return only unique values. I check for unique values with my find_value() function but it still return all the values.. Can someone explain me what happens? Thanks in advance
function find_value( $array, $searched_val ) {
foreach ( $array as $key => $val ) {
if ( $val === $searched_val ) {
return true;
}
if ( is_array( $val ) ) {
return find_value( $val, $searched_val );
}
}
return 0;
}
get_teams();
function get_teams() {
$people = get_data( 'some/file.json' );
$teams = [];
foreach ( $people as $person ) {
if ( ! find_value( $teams, $person['team'] ) ) {
$teams[] = [ 'text' => $person['team'] ];
}
}
return $teams;
}
This is sample input
Array
(
[0] => Array
(
[id] => 1
[name] => Friedrich Robel
[team] => WordPress
[position] => Frontend Developer
[salary] => 4400
)
[1] => Array
(
[id] => 2
[name] => Mr. Christop Veum
[team] => HTML
[position] => Manager
[salary] => 1200
)
[2] => Array
(
[id] => 3
[name] => Demarco Rippin
[team] => HTML
[position] => QA
[salary] => 4400
)
[3] => Array
(
[id] => 4
[name] => Felicia Farrell
[team] => HTML
[position] => QA
[salary] => 1200
)
[4] => Array
(
[id] => 5
[name] => Torrance Fritsch
[team] => HTML
[position] => Assistant Manager
[salary] => 2500
)
[5] => Array
(
[id] => 6
[name] => Erica Daugherty
[team] => Mail
[position] => Assistant Manager
[salary] => 500
)
)
And I want this output
Array
(
[0] => WordPress,
[1] => HTML,
[2] => Mail
)
Your problem is that you returns the result of the sub-search without testing it.
Instead of return find_value(...), you have to test its value.
function find_value( $array, $searched_val ) {
foreach ( $array as $key => $val ) {
if ( $val === $searched_val ) {
return true;
}
if ( is_array( $val ) ) {
// Here, you have to test the result before to return TRUE.
if (find_value( $val, $searched_val )) {
return true ;
}
}
}
return 0;
}
The fix is, as Syscall posted, to test the return of find_value(), and only return if it is true.
As your code is structured now, it will return either true or false from the recursion call, which will always be required, since you are storing your values as [ 'text' => $person['team'] ];
The way your code is designed, with the return 0 outside the foreach is only to return false in the case a match was not found. For this reason, returning a false from the contents of that conditional is premature. Your loop does not have a chance to iterate and test all the values of the passed array. This early returning results in duplicate array entries.
In any case, the results at the end will be an array of ['text' => '<job>'] array members. If you want only the job string, assign like this instead:
$teams[] = $person['team'];
If you make this change, the other is not required. Note that it also removes the need for recursion, as $teams will contain only an array of strings.
I'd like to filter an array which is created by converting XML to an array.
I'd like to remove all parent keys of arrays with key = 0 and an empty value (f.e. "InvalidKey"), but not the ones with a custom name and no value (f.e. "column").
I've already used array_filter (even in combination with array_map), but those functions will filter too few or too much information from the array.
I've also tried to create a loopable function to check if the current array has a key of 0 and an empty value, but I don't know how to get the parent key of the current array, f.e.:
Array
(
[InvalidKey] => Array
(
[0] => *NULL*
)
);
key($arrInput) = 0;
parent::key($arrInput) = "InvalidKey";
So, how to get from:
Array
(
[test] =>
[demo] => 524018
[column] =>
[xml] => Array
(
[Header] => Array
(
[0] => Array
(
[ValidKey] => Array
(
[0] => 1234
)
[InvalidKey] => Array
(
[0] =>
)
)
)
[Body] => Array
(
[0] => *NULL*
)
[Footer] => Array
(
[0] => Array
(
[ValidKey] => Array
(
[0] => I am valid
)
[MoreValidKey] => Array
(
[0] => I am valid too
)
[InvalidKey] => Array
(
[0] =>
)
)
)
)
)
To:
Array
(
[test] =>
[demo] => 524018
[column] =>
[xml] => Array
(
[Header] => Array
(
[0] => Array
(
[ValidKey] => Array
(
[0] => 1234
)
)
)
[Footer] => Array
(
[0] => Array
(
[ValidKey] => Array
(
[0] => I am valid
)
[MoreValidKey] => Array
(
[0] => I am valid too
)
)
)
)
)
PS: The used array key names are variable. For simplicity I used "(In)ValidKey". The array can be as much levels deep as possible, so I can't suffice with 2 for loops.
You need to write a custom script which checks all elements of your array.
Example:
<?php
$test = [
[
[
"asdf",
"",
0,
false,
true,
[
"asdf",
"",
[]
]
],
[]
],
[]
];
function removeEmptyElements(array $array)
{
foreach ($array as $key => $value) {
if (is_array($value))
$value = removeEmptyElements($value);
if (empty($value) && false !== $value && 0 !== $value)
unset($array[$key]);
else
$array[$key] = $value;
}
return $array;
}
print_r(removeEmptyElements($test));
I have an array like this:
Array
(
[0] => Array
(
[settingID] => 1
[name] => audioCueDistance
[setValue] => false
)
[1] => Array
(
[settingID] => 2
[name] => audioCueDistanceToGo
[setValue] => true
)
[2] => Array
(
[settingID] => 3
[name] => audioCues
[setValue] => true
)
[3] => Array
(
[settingID] => 4
[name] => audioCueStyle
[setValue] => default
)
[4] => Array
(
[settingID] => 5
[name] => audioCueTime
[setValue] => true
)
[5] => Array
(
[settingID] => 6
[name] => isMetric
[setValue] => true
)
How can I get individual values from key for example, I would like to output the setValue of isMetric.
Thanks
foreach ($foo as $bar) {
if ($bar['name'] == "isMetric") {
// Use setValue here
}
}
From what I understand you want to do something like $myArray['isMetric']['setValue'].
As your array is not in that form you need to map it that way.
$myArray = array(
array(
'settingID'=>6,
'name'=>'isMetric',
'value'=>true
)
);
$myAssocArray = array_reduce($myArray, function($carry, $item){
$carry[$item['name']] = $item;
return $carry;
}, array());
echo $myAssocArray['isMetric']['setValue'];
Run this code here: https://repl.it/CZ3R
foreach($array as $element)
{
if($element['name'] = 'isMetric')
return $element['setValue'];
}
throw new \Exception('isMetric Not Found.');
I have the following Two array results coming from MySQL query result.
Array One (Orignal-Data):
Array
(
[w_a] => Array
(
[0] => Array
(
[cod] => CRR
[pr] => LL
[aid] => VM2254
[gender] => m
[title] =>
)
)
[w_a_ml] => Array
(
)
[w_a_ol] => Array
(
)
[w_a_rl] => Array
(
[0] => Array
(
[rol] => 1
)
)
)
Array Two (Changed-Data)
Array
(
[w_a] => Array
(
[0] => Array
(
[cod] => CRR
[pr] => LL
[aid] => VM2254
[gender] => f
[title] => Mr
)
)
[w_a_ml] => Array
(
[0] => Array
(
[wl] => 255
[care] => Sahan
[heigh] =>
[adam] =>
[instance] => Look
)
)
[w_a_ol] => Array
(
)
[w_a_rl] => Array
(
[0] => Array
(
[rol] => 1
)
)
)
What I wan to achieve from the above two array is to compare and show the result in table format or Inside Form To each Field (Original and New-Change) like below.
FiledsN Original New Change
title Empty Mr
Wl Empty 255
Care Empty Sahan
gender m f
instance Empty Look
I've tried making the array a single array using this function:
function array_single($arr) {
if (!is_array($arr)) {
return FALSE;
}
$res = array();
foreach ($arr as $keys => $values) {
if (is_array($values)) {
$res = array_merge($res, array_single($values));
} else {
$res[$keys] = $values;
}
}
return $res;
}
And then comparing using this:
function diff($new,$old) {
$del=array_diff_assoc($old,$new);
$add=array_diff_assoc($new,$old);
return $diff=array("old"=>$del, "new"=>$add);
}
What is a function I can use that's basically the opposite of doing
if(strpos($array['some_key'], $value)!==false) {
that means there's a match and confinue
}
I basically want to loop through two arrays and grab the ones that don't have a match to the $value in $array.
$array =
Array
(
[0] => GPPZ20
[1] => GPPZ45
[2] => GPPZ75
[3] => GPPZH20
[4] => GPPZH45
)
$codes =
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH20SWYE4A2VZU
[amount] => 20
)
)
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH2077434178J6
[amount] => 20
)
)
Array
(
[0] => Array
(
[count] => 17
[code] => PMMC4
[amount] => 25
)
)
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH2052910M8V62
[amount] => 20
)
)
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH45B3116LD1VW
[amount] => 45
)
)
so what i want to do is grab all the ones in the $codes array where the $codes['code'] value does not match any of the ones in the $array value.
right now i have the ones that match and grabbing those by doing
foreach($codes as $code) {
foreach($array as $key=>$value) {
if(strpos($code['code'], $value)!==false) {
//it matches grab those values
}
}
}
I basically now need something like this to grab the ones that do not match
You should use array_filter function - http://php.net/manual/en/function.array-filter.php e.g.:
function myFilter($val){ return strpos('foo', $val) === false; }
$array = array("foobar", "foo", "bar");
var_dump(array_filter($array, myFilter));
You can also use preg_match method instead of strpos.