how to find empty value for specific key in multidimensional array - php

i have array like this
<?php
$array =
array
(
array (
0 => 1,
1 => 'php',
2 => 11,
3 => 11,
4 => 11,
5 => 11,
6 => 11,
),
array (
0 => 1,
1 => 'php',
2 => 11,
3 => 11,
4 => 11,
5 => 11,
6 => ,
),
);
and i want to search in this multi-array to find if the key [6] => is empty.if it was empty in any array return false so how to do this
foreach($array as $item)
{
foreach($item as $key=>$value)
{
print($key);
if($key=="6" && $value==NULL)
{
echo "found";
return false;
}else{
echo "not found";
return true;
}
}
}

$empty = false;
foreach($array as $item)
{
if(empty($item[6]))
{
$empty=true;
break;
}
}
return $empty;

First, it's recommended to use only one return in a function, so define a boolean value, and turn it to TRUE when the condition satisfies.
Second, use break to stop the cycle(s) running (saves runtime)
http://php.net/manual/en/control-structures.break.php
Third, check keys and values appropriately. Your keys are numeric and you are comparing to a string "6". Use empty() if you are interested in that the value is an empty-ish value. Be aware that a numeric zero is an empty value. If you are specifically interested in NULL value, use === operator
http://php.net/manual/en/function.empty.php
http://php.net/manual/en/language.operators.comparison.php
+1 See Yoda-style conditions
http://en.wikipedia.org/wiki/Yoda_conditions
++1 Use K&R style indent, or don't use it. But do not try! ;)
http://en.wikipedia.org/wiki/Indent_style#K.26R_style
$found = false;
foreach ($array as $item) {
foreach ($item as $key => $value) {
print($key);
if (6 == $key && NULL === $value) { // or use 'empty($value)'
echo "found";
$found = true;
break 2;
} else {
echo "not found";
}
}
}
return !$found;

Here's an alternate for PHP >= 5.5.0 that checks for '', 0, null and false:
return !array_diff($six = array_column($array, 6), array_filter($six));

Related

Fastest return TRUE if all values of a column of a multidimensional array is_numeric

Are there faster methods to check the existence of a number (not null) in one
column of a multidimensional array in php (for instance, number9)?
Attempt:
The if statement below seems to be working okay.
$arr=Array(
[0] => Array
(
[date] => 2019-01-16
[number1] => 20.4
[number2] => 20.54
[number3] => 19.71
[number4] => 19.73
[number5] => 70849266
[number6] => 70849266
[number7] => -0.65
[number8] => -3.189
[number9] => 20.0902
[string1] => Jan 16
[number10] => 0.047796070100903
)
.
.
.
[21] => Array
(
[date] => 2019-01-17
[number1 => 19.49
[number2] => 20.51
[number3] => 19.02
[number4] => 20.25
[number5] => 85018421
[number6] => 85018421
[number7] => 0.52
[number8] => 2.636
[number9] => 19.7988
[string1] => Jan 17
[number10] => 0.075411577270313
)
);
function isNumeric($var){
if (is_numeric($var)){
return true;
} else {
return false;
}
}
if((array_walk(array_column($arr, "number8"), 'isNumeric'))!=1)
Here are my ideas.
First is to just filter the array for numeric only values and compare to the original:
function with_array_filter($arr) {
return $arr == array_filter($arr, 'is_numeric');
}
The second example uses casting to a float and then back to string, keep in mind that this is not going to be accurate for very big numbers, however seems to be the fastest (if you care about that at all):
function is_numeric_array_with_cast($arr) {
foreach ($arr as $b) {
if ($b != (string)(float)$b) {
return false;
}
}
return true;
}
However probably the simplest solution is to just foreach the array inside a function and return early:
function is_numeric_array_with_func($arr) {
foreach ($arr as $b) {
if (!is_numeric($b)) {
return false;
}
}
return true;
}
Benchmarked with an array of 20 elements over 100000 iterations on PHP 7.2:
$falseArray = [
'1',
2.5,
-10,
32,
11.0,
2.5,
100101221,
345,
-10,
'-10',
'+10',
'10',
12,
PHP_INT_MAX,
PHP_INT_MAX + 1.4e5,
'-10',
null,
'a',
'5',
5
];
Matt Fryer
Time: 4.8473789691925
is_numeric_array_with_func
Time: 4.0416791439056
is_numeric_array_with_cast
Time: 3.2953300476074
with_array_filter
Time: 3.99729180336
AS I said in the comments:
The if statement below seems to be working okay
However, given the code you posed I doubt that: lets look at it.
function isNumeric($var){ ... }
if(array_walk(array_column($arr, "number8"), 'isNumberic'))!=1
The first and most obvious things are these 2
isNumberic vs isNumeric, which is a fatal undefined function error (spelling/typo).
)!=1 then this is outside of the actual condition, or put another way if(...){ !=1 }
Let's assume those are just typos in the question. Even if your code was free of the 2 "defects" I mentioned above you would still have this problem, array_walk works by reference and simply returns true (always). Pass by reference updates the "Original" variable without returning a copy of it (in the case of array_walk)
http://php.net/manual/en/function.array-walk.php
Return Values
Returns TRUE.
Which of course just makes your condition pass no matter what. So you should always test both the passing and the failing of the condition (As I did by placing some "canned" bad data in there). This way I know for 100% sure exactly how my code behaves.
Others have posted how to correct this, but not what you did wrong. But just for the sake of completeness I will post an answer anyway.
$arr = array (
0 =>
array (
'date' => '2019-01-16',
'number1' => 20.4,
'number2' => 20.54,
'number3' => 19.71,
'number4' => 19.73,
'number5' => 70849266,
'number6' => 70849266,
'number7' => -0.65,
'number8' => -3.189,
'number9' => 20.0902,
'string1' => 'Jan16',
'number10' => 0.047796070100903
),
array (
'date' => '2019-01-16',
'number1' => 20.4,
'number2' => 20.54,
'number3' => 19.71,
'number4' => 19.73,
'number5' => 70849266,
'number6' => 70849266,
'number7' => -0.65,
'number8' => 'foo',#intentially not number
'number9' => 20.0902,
'string1' => 'Jan16',
'number10' => 0.047796070100903
),
);
$col = array_column($arr, "number8");
$col1 = array_filter($col, 'is_numeric');
if($col != $col1){
echo "not the same\n";
}
Output:
not the same
Sandbox
I should mention, there is no "need" to count these, as PHP can compare complex objects for equality. As we are comparing the same "root" array ($col in this example) with itself after (possibly) removing some elements, if no elements were removed then both arrays should be not only the same length but also "Identical".
Also if you want to do it in one line (inside the condition) you can do this:
if( ($col = array_column($arr, "number8")) && $col != array_filter($col, 'is_numeric')){
echo "not the same\n";
}
It's a bit harder to read, and pay attention to $col = array_column assignment.
Use a foreach loop:
$bool = true;
foreach ($arr as $row)
{
if (!is_numeric($row['number8']))
{
$bool = false;
break;
}
}
Thanks a million everyone, for your great answers!
On my PC, I tried your four functions in PHP 5.5.38 with ~5000 iterations and total times are:
"is_numeric_array_with_cast total time is 0.44153618812561"
"with_array_filter total time is 0.21628260612488"
"is_numeric_array_with_func total time is 0.14269280433655"
"is_numeric_matt_fryer total time is 0.155033826828"
$t1=$t2=$t3=$t4=0;
foreach($arrs as $k=>$arr){
$s1=microtime(true);
is_numeric_array_with_cast(array_column($arr, "number8"));
$e1=microtime(true)-$s1;
$t1=$t1+$e1;
$s2=microtime(true);
with_array_filter(array_column($arr, "number8"));
$e2=microtime(true)-$s2;
$t2=$t2+$e2;
$s3=microtime(true);
is_numeric_array_with_func(array_column($arr, "number8"));
$e3=microtime(true)-$s3;
$t3=$t3+$e3;
$s4=microtime(true);
is_numeric_matt_fryer(array_column($arr, "number8"),"number8");
$e4=microtime(true)-$s4;
$t4=$t4+$e4;
}
function is_numeric_array_with_cast($arr) {
foreach ($arr as $b) {
if ($b != (string)(float)$b) {
return false;
}
}
return true;
}
function with_array_filter($arr) {
return $arr == array_filter($arr, 'is_numeric');
}
function is_numeric_array_with_func($arr) {
foreach ($arr as $b) {
if (!is_numeric($b)) {
return false;
}
}
return true;
}
function is_numeric_matt_fryer($arr,$str){
$bool = true;
foreach ($arr as $row)
{
if (!is_numeric($row[$str]))
{
$bool = false;
}
}
return $bool;
}

Get value of specific $_POST array

name="qty<?php echo $key?>"
foreach ($_POST as $items => $value)
{
// check qty >1???
echo $key, ' => ', $value, '<br />';
}
How do I show only items which their values of [qty1]=>value,[qty2]=>value... >0 ?
Just use combination of array_filter and print_r.
$_POST = [
'notme' => 12,
'qty1' => 1,
'qty2' => 20,
'qty3' => -1,
'qty4' => 0,
'qty5' => 30
];
print_r(
array_filter($_POST, function ($value, $key) {
// check key starts with 'qty' and its value is greater than one
return strpos($key, 'qty') === 0 && $value > 1;
}, ARRAY_FILTER_USE_BOTH)
);
// Array ( [qty2] => 20 [qty5] => 30 )
Why don't you add a check in your loop and then get a filtered output like this:
if your POST request is not a multidimensional array then use this:
$output = array_filter($_POST, function ($value, $key) {
// check your keys start with 'qty' and its value is greater than one
return strpos($key, 'qty') === 0 && $value > 1;
}, ARRAY_FILTER_USE_BOTH);
// display final output
print_r($output);
If your POST request is a multidimensional array then use this:
$output = array(); // declare a blank array to store filtered output
foreach($_POST as $row)
{
if (is_array($row))
{
// this code is because qty is dynamic and we need to check it for all.
foreach($row as $key => $value)
{
if ("qty" == substr($key, 0, 3) && $value > 0)
{
$output[] = $row;
}
}
}
}
// display final array.
print_r($output);
Hope that will help!

Get corresponding array values from same index [duplicate]

using array_search in a 1 dimensional array is simple
$array = array("apple", "banana", "cherry");
$searchValue = "cherry";
$key = array_search($searchValue, $array);
echo $key;
but how about an multi dimensional array?
#RaceRecord
[CarID] [ColorID] [Position]
[0] 1 1 3
[1] 2 1 1
[2] 3 2 4
[3] 4 2 2
[4] 5 3 5
for example i want to get the index of the car whose position is 1. How do i do this?
In php 5.5.5 & later versions,
you can try this
$array_subjected_to_search =array(
array(
'name' => 'flash',
'type' => 'hero'
),
array(
'name' => 'zoom',
'type' => 'villian'
),
array(
'name' => 'snart',
'type' => 'antihero'
)
);
$key = array_search('snart', array_column($array_subjected_to_search, 'name'));
var_dump($array_subjected_to_search[$key]);
Output:
array(2) { ["name"]=> string(5) "snart" ["type"]=> string(8) "antihero" }
working sample : http://sandbox.onlinephpfunctions.com/code/19385da11fe0614ef5f84f58b6dae80bd216fc01
Documentation about array_column can be found here
function find_car_with_position($cars, $position) {
foreach($cars as $index => $car) {
if($car['Position'] == $position) return $index;
}
return FALSE;
}
You can try this
array_search(1, array_column($cars, 'position'));
Hooray for one-liners!
$index = array_keys(array_filter($array, function($item){ return $item['property'] === 'whatever';}))[0];
Let's make it more clear:
array_filter(
$array,
function ($item) {
return $item['property'] === 'whatever';
}
);
returns an array that contains all the elements that fulfill the condition in the callback, while maintaining their original array keys. We basically need the key of the first element of that array.
To do this we wrap the result in an array_keys() call and get it's first element.
This specific example makes the assumption that at least one matching element exists, so you might need an extra check just to be safe.
I basically 'recreated' underscore.js's findWhere method which is to die for.
The function:
function findWhere($array, $matching) {
foreach ($array as $item) {
$is_match = true;
foreach ($matching as $key => $value) {
if (is_object($item)) {
if (! isset($item->$key)) {
$is_match = false;
break;
}
} else {
if (! isset($item[$key])) {
$is_match = false;
break;
}
}
if (is_object($item)) {
if ($item->$key != $value) {
$is_match = false;
break;
}
} else {
if ($item[$key] != $value) {
$is_match = false;
break;
}
}
}
if ($is_match) {
return $item;
}
}
return false;
}
Example:
$cars = array(
array('id' => 1, 'name' => 'Toyota'),
array('id' => 2, 'name' => 'Ford')
);
$car = findWhere($cars, array('id' => 1));
or
$car = findWhere($cars, array(
'id' => 1,
'name' => 'Toyota'
));
I'm sure this method could easily reduce LOC. I'm a bit tired. :P
actually all array functions are designed for single dimension array.You always need to keep in mind that you are applying it on single dimension array.
function find_car_with_position($cars, $position) {
for($i=0;$i<count($cars);$i++){
if(array_search($search_val, $cars[$i]) === false){
// if value not found in array.....
}
else{
// if value is found in array....
}
}
}

Recursively check value empty or not

I have to check if items with keys 2, 3, 4, 5 in the sub array with index 0 are empty or not. If empty, I need to throw an exception.
If not empty, then move to next iteration, checking items with keys 3, 4, 5, 6 in the sub array with index 1, and so on.
Items with keys 0 and 1 will always be empty so nothings need to be done with them.
So, check for items with key > 1 then 4-4 pair check if anyone is empty and then throw an exception.
here is my code
$array = array(
array('0' => '','1' => '','2' => 'Wasatch standard','3' => 'Wasatch standard','4' => '3,5,2','5' => 'English','6' => '','7' => '','8' => ''),
array('0' => '','1' => '','2' => '','3' => 'ThisIsAtest','4' => 'Wasatch standard1','5' => '3,4,5','6' => 'English','7' => '','8' => ''),
array('0' => '','1' => '','2' => '','3' => '','4' => 'Wasatch standard1.1','5' => 'Wasatch standard1.1','6' => '2','7' => 'Mathematics','8' =>''),
);
for($i=0;$i<count($array);$i++){
checkRecursivelyIfEmpty($array[$i],array('2'+$i,'3'+$i,'4'+$i,'5'+$i));
}
function checkRecursivelyIfEmpty($value,$sumValue){
foreach ($sumValue as $k => $v) {
if(empty($value[$v])){
throw new Exception("Error Processing Request");
}
}
}
The following function first checks whether the input is an array indeed, then checks for the indexes to be empty. If not, then it throws an exception. Furthermore, it traverses the array to see if there are inner arrays. If so, then it recursively checks them as well. After the function there is a quick demonstration of usage.
function checkThings($inputArray, $subArray) {
//If it is not an array, it does not have the given indexes
if (!is_array($inputArray)) {
return;
}
//throws exception if one of the elements in question is not empty
foreach ($subArray as $key) {
if ((isset($inputArray[$key])) && (!empty($inputArray[$key]))) {
throw new Exception("My Exception text");
}
}
//checks for inner occurrences
foreach ($inputArray as $key => $value) {
if (is_array($inputArray[$key])) {
checkThings($inputArray[$key], $subArray);
}
}
}
//Calls checkThings for all elements
for ($index = 0; $index < count($myArray); $index++) {
checkThings($myArray[$index], array($index + 2, $index + 3, $index + 4, $index + 5));
}
Use foreach and check using empty($value)
foreach ($array as $key => $value) {
$value = trim($value);
if (empty($value))
echo "$key empty <br/>";
else
echo "$key not empty <br/>";
}
Assuming the array is named $a you can use this code. The outer loops iterates over all the arrays. The inner loop iterates from $i+2 to $i+5 (in the case of $i=0, you get 2, 3, 4 and 5) on the sub arrays. The function empty() checks that the item is set and not equal to false (for instance, an empty string).
for($i=0; $i<count($a); $++)
for($j=$i+2; $j<$i+6; $j++)
if(empty($a[$i][$j]))
//Raise an error!

How to check if a specific value exists at a specific key in any subarray of a multidimensional array?

I need to search a multidimensional array for a specific value in any of the indexed subarrays.
In other words, I need to check a single column of the multidimensional array for a value. If the value exists anywhere in the multidimensional array, I would like to return true otherwise false
$my_array = array(
0 => array(
"name" => "john",
"id" => 4
),
1 => array(
"name" => "mark",
"id" => 152
),
2 => array(
"name" => "Eduard",
"id" => 152
)
);
I would like to know the fastest and most efficient way to check if the array $my_array contains a value with the key "id". For example, if id => 152 anywhere in the multidimensional array, I would like true.
Nothing will be faster than a simple loop. You can mix-and-match some array functions to do it, but they'll just be implemented as a loop too.
function whatever($array, $key, $val) {
foreach ($array as $item)
if (isset($item[$key]) && $item[$key] == $val)
return true;
return false;
}
The simplest way is this:
$my_array = array(
0 => array(
"name" => "john",
"id" => 4
),
1 => array(
"name" => "mark",
"id" => 152
),
2 => array(
"name" => "Eduard",
"id" => 152
)
);
if (array_search(152, array_column($my_array, 'id')) !== FALSE) {
echo 'FOUND!';
} else {
echo 'NOT FOUND!';
}
** PHP >= 5.5
simply u can use this
$key = array_search(40489, array_column($userdb, 'uid'));
Let's suppose this multi dimensional array:
$userdb=Array
(
(0) => Array
(
(uid) => '100',
(name) => 'Sandra Shush',
(url) => 'urlof100'
),
(1) => Array
(
(uid) => '5465',
(name) => 'Stefanie Mcmohn',
(pic_square) => 'urlof100'
),
(2) => Array
(
(uid) => '40489',
(name) => 'Michael',
(pic_square) => 'urlof40489'
)
);
$key = array_search(40489, array_column($userdb, 'uid'));
Here is an updated version of Dan Grossman's answer which will cater for multidimensional arrays (what I was after):
function find_key_value($array, $key, $val)
{
foreach ($array as $item)
{
if (is_array($item) && find_key_value($item, $key, $val)) return true;
if (isset($item[$key]) && $item[$key] == $val) return true;
}
return false;
}
If you have to make a lot of "id" lookups and it should be really fast you should use a second array containing all the "ids" as keys:
$lookup_array=array();
foreach($my_array as $arr){
$lookup_array[$arr['id']]=1;
}
Now you can check for an existing id very fast, for example:
echo (isset($lookup_array[152]))?'yes':'no';
A good solution can be one provided by #Elias Van Ootegan in a comment that is:
$ids = array_column($array, 'id', 'id');
echo isset($ids[40489])?"Exist":"Not Exist";
I tried it and worked for me, thanks buddy.
Edited
Note: It will work in PHP 5.5+
TMTOWTDI. Here are several solutions in order of complexity.
(Short primer on complexity follows):O(n) or "big o" means worst case scenario where n means the number of elements in the array, and o(n) or "little o" means best case scenario. Long discrete math story short, you only really have to worry about the worst case scenario, and make sure it's not n ^ 2 or n!. It's more a measure of change in computing time as n increases than it is overall computing time. Wikipedia has a good article about computational aka time complexity.
If experience has taught me anything, it's that spending too much time optimizing your programs' little-o is a distinct waste of time better spent doing something - anything - better.
Solution 0: O(n) / o(1) complexity:
This solution has a best case scenario of 1 comparison - 1 iteration thru the loop, but only provided the matching value is in position 0 of the array. The worst case scenario is it's not in the array, and thus has to iterate over every element of the array.
foreach ($my_array as $sub_array) {
if (#$sub_array['id'] === 152) {
return true;
}
}
return false;
Solution 1: O(n) / o(n) complexity:
This solution must loop thru the entire array no matter where the matching value is, so it's always going to be n iterations thru the array.
return 0 < count(
array_filter(
$my_array,
function ($a) {
return array_key_exists('id', $a) && $a['id'] == 152;
}
)
);
Solution 2: O(n log n) / o(n log n) complexity:
A hash insertion is where the log n comes from; n hash insertions = n * log n. There's a hash lookup at the end which is another log n but it's not included because that's just how discrete math works.
$existence_hash = [];
foreach ($my_array as $sub_array) {
$existence_hash[$sub_array['id']] = true;
}
return #$existence_hash['152'];
I came upon this post looking to do the same and came up with my own solution I wanted to offer for future visitors of this page (and to see if doing this way presents any problems I had not forseen).
If you want to get a simple true or false output and want to do this with one line of code without a function or a loop you could serialize the array and then use stripos to search for the value:
stripos(serialize($my_array),$needle)
It seems to work for me.
As in your question, which is actually a simple 2-D array wouldn't it be better? Have a look-
Let say your 2-D array name $my_array and value to find is $id
function idExists($needle='', $haystack=array()){
//now go through each internal array
foreach ($haystack as $item) {
if ($item['id']===$needle) {
return true;
}
}
return false;
}
and to call it:
idExists($id, $my_array);
As you can see, it actually only check if any internal index with key_name 'id' only, have your $value. Some other answers here might also result true if key_name 'name' also has $value
I don't know if this is better or worse for performance, but here is an alternative:
$keys = array_map(function($element){return $element['id'];}, $my_array);
$flipped_keys = array_flip($keys);
if(isset($flipped_keys[40489]))
{
// true
}
You can create a queue of sub-arrays and loop each:
function existsKeyValue($myArray, $key, $value) {
$queue = [$myArray]; //creating a queue of a single element, which is our outermost array
//when we reach the count of the queue we looped all inner loops as well and failed to find the item
for ($index = 0; $index < count($queue); $index++) {
//Looping the current array, finding the key and the value
foreach ($queue[$index] as $k => &$v) {
//If they match the search, then we can return true
if (($key === $k) && ($value === $v)) {
return true;
}
//We need to make sure we did not already loop our current array to avoid infinite cycles
if (is_array($v)) $queue[]=$v;
}
}
return false;
}
$my_array = array(
0 => array(
"name" => "john",
"id" => 4
),
1 => array(
"name" => "mark",
"id" => 152
),
2 => array(
"name" => "Eduard",
"id" => 152
)
);
echo var_dump(existsKeyValue($my_array, 'id', 152));
array_column returns the values of a single column of an array and we can search for a specific value for those via in_array
if (in_array(152, array_column($my_array, 'id'))) {
echo 'FOUND!';
} else {
echo 'NOT FOUND!';
}
Try with this below code. It should be working fine for any kind of multidimensional array search.
Here you can see LIVE DEMO EXAMPLE
function multi_array_search($search_for, $search_in) {
foreach ($search_in as $element) {
if ( ($element === $search_for) ){
return true;
}elseif(is_array($element)){
$result = multi_array_search($search_for, $element);
if($result == true)
return true;
}
}
return false;
}
You can use this with only two parameter
function whatever($array, $val) {
foreach ($array as $item)
if (isset($item) && in_array($val,$item))
return 1;
return 0;
}
different between isset vs array_key_exits
What's the difference between isset() and array_key_exists()?
different between == vs === How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
function specificValue(array $array,$key,$val) {
foreach ($array as $item)
if (array_key_exits($item[$key]) && $item[$key] === $val)
return true;
return false;
}
function checkMultiArrayValue($array) {
global $test;
foreach ($array as $key => $item) {
if(!empty($item) && is_array($item)) {
checkMultiArrayValue($item);
}else {
if($item)
$test[$key] = $item;
}
}
return $test;
}
$multiArray = array(
0 => array(
"country" => "",
"price" => 4,
"discount-price" => 0,
),);
$test = checkMultiArrayValue($multiArray);
echo "<pre>"
print_r($test);
Will return array who have index and value
I wrote the following function in order to determine if an multidimensional array partially contains a certain value.
function findKeyValue ($array, $needle, $value, $found = false){
foreach ($array as $key => $item){
// Navigate through the array completely.
if (is_array($item)){
$found = $this->findKeyValue($item, $needle, $value, $found);
}
// If the item is a node, verify if the value of the node contains
// the given search parameter. E.G.: 'value' <=> 'This contains the value'
if ( ! empty($key) && $key == $needle && strpos($item, $value) !== false){
return true;
}
}
return $found;
}
Call the function like this:
$this->findKeyValue($array, $key, $value);

Categories