Compare the keys and values of three arrays with "array_diff_ukey" - php

Can somebody explain, what is 1 and -1 in this code: ($a>$b)?1:-1; ?
I know the Array ( [c] => blue ) is returning because the key c is not exist in $a2 and key_compare_func need to return number smaller, equal or bigger then 0.
But I don't understand, how I get the Array ( [c] => blue ), when the key_compare_func returns 0, 1 and -1:
function myfunction($a,$b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_diff_ukey($a1,$a2,"myfunction");

As you can see in array-diff-ukey documentation the "key_compare_func" need to return number smaller, equal or bigger then 0. the numbers 1 and -1 are just example for this results.
In your case you can simply use strcmp as it return the same logic.
You have Array ( [c] => blue ) in the return because the key c is not exist in the $a2 array as it say:
Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.
Edited
Specifically in array-diff-ukey you only need the return 0 because that the way this function is decided the keys are the same so in your example you can define it as:
function myfunction($a,$b) {
if ($a === $b)
return 0;
return 1; // or -1 or 3 or -3 **just not 0**
}
Consider that as the logic behind array-diff-ukey:
array function array-diff-ukey($a1, $a2, $compareFunction) {
$arr = array(); // init empty array
foreach($a1 as $key1 => $value1) { // for each key in a1
$found = false;
foreach($a1 as $key2 => $value2) { //search for all keys in a2
if ($compareFunction($key1, $key2) == 0)
$found = true; // found a key with the same
}
if ($found === false) // add the element only if non is found
$arr[$key1] = $value1;
}
return $arr;
}

If ($a>$b) is true (right after the ?) - you return 1. else (right after the :) will return -1.
It's a short way of writing this:
if ($a>$b) {
return 1;
} else {
return -1;
}

It is the ternary operator in PHP. You can say it as shorthand If/Else. Here is an example:
/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // if $var greater than 2
// return true
// else false
If it is being difficult for you to understand, you can change it with:
if ($a===$b)
{
return 0;
}
else if($a > $b)
{
return 1;
}
else
{
return -1;
}

Related

Exactly one occurence of value in array

I'm trying to find a native php function which returns true if there is exactly one occurrence of any given element in an array.
For example:
$searchedForValue = 3;
$array1 = [1,2,3,4,5,6];
$array2 = [1,2,3,3,4,5];
$array3 = [1,2,4,5,6];
oneOccurrence($array1,$searchedForValue);
oneOccurrence($array2,$searchedForValue);
oneOccurrence($array3,$searchedForValue);
This should return:
true
false
false
Cheers
You should use array_count_values() here.
$array_values = array_count_values($array2);
This will return an array. Key denotes each element of $array2 and value denotes frequency of each element.:
Array (
[1] => 1
[2] => 1
[3] => 2 // Denotes 3 appears 2 times
[4] => 1
[5] => 1
)
if (#$array_values[$searchedForValue] == 1) {
echo "True";
} else {
echo "False";
}
This does what you want. Bare in mind, echoing oneOccurence won't output 'true' or 'false', it returns a boolean.
function oneOccurrence ($arr, $val) {
$occurrences = [];
foreach ($arr as $v) {
if ($v == $val) {
$occurrences[] = $v;
}
}
return count($occurrences) == 1;
}
P.S echo doesn't need brackets as it's a PHP construct.
A small utility function - count array values using array_count_values and then if the search key exists and if its count is exactly 1, return true otherwise false:
function oneOccurrence($array, $searchValue) {
$array = array_count_values($array);
return (array_key_exists($searchValue, $array) && $array[$searchValue]==1)?true:false;
}
Check EVAL
Since you are searching for one occurrence: http://codepad.org/cZImb4FI
Use array_count_values()
<?php
$searchedForValue = 3;
$array1 = array(1,2,3,4,5,6);
$array2 = array(1,2,3,3,4,5);
$array3 = array(1,2,4,5,6);
$arr = array($array1,$array2 ,$array3);
function array_count_values_of($value, $array) {
$counts = array_count_values($array);
return $counts[$value];
}
foreach($arr as $ar){
if( array_count_values_of($searchedForValue,$ar)==1){echo "true";}else{echo "false";}
}
?>
output
true false false
//In Php in_array() function is provided..
$searchedForValue = 3;
$array1 = array(1,2,3,4,5,6);
$array2 = array(1,2,3,3,4,5);
$array3 = array(1,2,4,5,6);
if(in_array($searchedForValue,$array1))
{
echo "true";
}
else
{
echo "false";
}
if(in_array($searchedForValue,$array2))
{
echo "true";
}
else
{
echo "false";
}
if(in_array($searchedForValue,$array3))
{
echo "true";
}
else
{
echo "false";
}

Why does array_diff_uassoc compares keys whose value do not match

I just read that question about strange php behaviour, and even though I could research a bit more, I'm nowhere near understanding it.
I assume the reader has read the original question and is aware of OP's code block and sample, but in short, OP is trying to compare those two arrays, and while the result is good, the compare function seems to be called erratically:
$chomik = new chomik('a');
$a = array(5, $chomik, $chomik, $chomik);
$b = array($chomik, 'b', 'c', 'd');
array_diff_uassoc($a, $b, 'compare');
The documentation is a bit obscure... but it does state that:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
As I understand it, that means that the compare() function should be more like this:
function compare($a, $b) {
echo("$a : $b<br/>");
if($a === $b) return 0;
else if ($a > $b) return 1;
else return -1;
}
however this still gives very strange results, with even more "duplicates"
1 : 0
1 : 2
3 : 1
2 : 1
3 : 2
1 : 0
1 : 2
3 : 1
2 : 1
3 : 2
0 : 0
1 : 0
1 : 1
2 : 0
2 : 1
2 : 2
3 : 0
3 : 1
3 : 2
3 : 3
faced with many doubts, I read the compat php function, and the part where the check actually happens is interesting:
foreach ($args[0] as $k => $v) {
for ($i = 1; $i < $array_count; $i++) {
foreach ($args[$i] as $kk => $vv) {
if ($v == $vv) { // compare keys only if value are the same
$compare = call_user_func_array($compare_func, array($k, $kk));
if ($compare == 0) {
continue 3; // value should not be added to the result
}
}
}
}
$result[$k] = $v;
}
here's the actual source (per comment)
The way this code executes the compare function should not be outputting the result we see. Foreach is not able to move back and forth in the keys (AFAIK???), as seems to be the case in the order of the first key here:
1 : 2
3 : 1
2 : 1
moreover, it shouldn't check the keys if the value do not match, so why do all these are checked:
1 : 2
3 : 1
2 : 1
3 : 2
etc...
How can the topmost foreach() in the source code loop back and forth through the keys?!
Why are keys whose values do not match still compared?
Do foreach loops actually continue executing even when they've been continued?
Is this an example of concurrency? can call_user_func_array somehow be launched and actually execute the echo("$a : $b<br/>"); of the compare function not in the same order they were "launched"??
I believe you've pinpointed a bug, my friend. I just ran the code in the question you referenced, and sure enough, it compared keys for values that weren't the same. However, I wanted to test if the source code itself contained the mistake, so I added the official source for array_diff_uassoc this to the top his code, inside my own namespace:
<?php
namespace mine;
// Code obtained from https://pear.php.net/reference/PHP_Compat-latest/__filesource/fsource_PHP_Compat__PHP_Compat-1.6.0a3CompatFunctionarray_diff_uassoc.php.html
function array_diff_uassoc()
{
// Sanity check
$args = func_get_args();
if (count($args) < 3) {
user_error('Wrong parameter count for array_diff_uassoc()', E_USER_WARNING);
return;
}
// Get compare function
$compare_func = array_pop($args);
if (!is_callable($compare_func)) {
if (is_array($compare_func)) {
$compare_func = $compare_func[0] . '::' . $compare_func[1];
}
user_error('array_diff_uassoc() Not a valid callback ' .
$compare_func, E_USER_WARNING);
return;
}
// Check arrays
$array_count = count($args);
for ($i = 0; $i !== $array_count; $i++) {
if (!is_array($args[$i])) {
user_error('array_diff_uassoc() Argument #' .
($i + 1) . ' is not an array', E_USER_WARNING);
return;
}
}
// Compare entries
$result = array();
foreach ($args[0] as $k => $v) {
for ($i = 1; $i < $array_count; $i++) {
foreach ($args[$i] as $kk => $vv) {
if ($v == $vv) {
// echo ("$v\n");
// echo ("$vv\n");
// echo ("$k\n");
// echo ("$kk\n");
// die();
$compare = call_user_func_array($compare_func, array($k, $kk));
if ($compare == 0) {
continue 3;
}
}
}
}
$result[$k] = $v;
}
return $result;
}
class chomik {
public $state = 'normal';
public $name = 'no name';
public function __construct($name) {
$this->name = $name;
}
public function __toString() {
return $this->name . " - " . $this->state;
}
}
function compare($a, $b) {
echo("$a : $b\n");
if($a != $b) {
return 0;
}
else return 1;
}
$chomik = new chomik('a');
$a = array(5, $chomik, $chomik, $chomik);
$b = array($chomik, 'b', 'c', 'd');
array_diff_uassoc($a, $b, 'mine\compare');
This time, it only compared keys for values that were equal:
1 : 0
2 : 0
3 : 0
Strange, huh?
From this comment by powerlord:
Judging from the fact that the custom compare function asks you to return -1; 0; or 1, it seems like its doing a sort either before or at the same time as a comparison between the two arrays.
I was encouraged to go and read the actual php_array_diff() source, the registered function for array_diff_uassoc(), and discovered it uses the compare function a lot of times, which I wrongly interpreted as foreach going back and forth through the keys.
How can the topmost foreach() in the source code loop back and forth through the keys?!
it doesn't. It's just that the user-supplied function
diff_data_compare_func = php_array_user_compare;
is used multiple times to sort and evaluate the data.
[...]
zend_sort((void *) lists[i], hash->nNumOfElements,
sizeof(Bucket), diff_data_compare_func, (swap_func_t)zend_hash_bucket_swap);
[...]
while (Z_TYPE(ptrs[i]->val) != IS_UNDEF && (0 < (c = diff_data_compare_func(ptrs[0], ptrs[i])))) {
[...]
if (diff_data_compare_func(ptrs[0], ptr) != 0) {
[...]
Why are keys whose values do not match still compared?
It is true the compat pear code posted in the question hints that if the valus do not match, the compare function should not even be run, but php_array_diff() acts differently.
Do foreach loops actually continue executing even when they've been continued?
Is this an example of concurrency?
Nonsense. If I were you, I'd edit that out.

need help in get values from array

Hi need help getting values from array
first i have get consecutive values from below array eg=>id:1,2 ignore if consecutive values doesn`t not exits
after getting values find the differences between the bids of consecutive values , then sum up all the values of bids(consecutive values) ??
$bidperpage = array([0]=>array(['id']=>1,['bid']='10',['page']='5'),
[1]=>array(['id']=>2,['bid']='15',['page']='5'),
[2]=>array(['id']=>9,['bid']='20',['page']='2'),
[3]=>array(['id']=>3,['bid']='30',['page']='7'),
[4]=>array(['id']=>4,['bid']='40',['page']='7'),
[5]=>array(['id']=>5,['bid']='50',['page']='9'),
[6]=>array(['id']=>6,['bid']='60',['page']='4'),
[7]=>array(['id']=>8,['bid']='70',['page']='4'));
function checkconsecutivevalue($array) {
$ret = array();
$temp = array();
foreach($array as $val) {
if(next($array) == ($val + 1))
$temp[] = $val;
else
if(count($temp) > 0) {
$temp[] = $val;
$ret[] = $temp[0].':'.end($temp);
$temp = array();
}
else
$ret[] = $val;
}
return $ret;
}
If nothing else, your array definition is a flat-out syntax error. It should be
$bidperpage = array(
0 => array('id' => 1, 'bid' => '10', 'page' => '5'),
etc...
);
Note the proper use of the => operator, and the LACK of [] braces.
The easiest way to do this, to my mind is using usort:
$bidperpage = array(array('id'=>1,'bid'=>'10','page'=>'5'),
array('id'=>2,'bid'=>'15','page'=>'5'),
array('id'=>9,'bid'=>'20','page'=>'2'),
array('id'=>3,'bid'=>'30','page'=>'7'),
array('id'=>4,'bid'=>'40','page'=>'7'),
array('id'=>5,'bid'=>'50','page'=>'9'),
array('id'=>11,'bid'=>'60','page'=>'q'),
array('id'=>8,'bid'=>'70','page'=>'4'));
//Sort function
function sortById($a,$b)
{
if ($a['id'] === $b['id'])
{
return 0;
}
return ($a['id'] > $b['id'] ? 1 : -1);
}
usort($bidperpage,'sortById');
More info: See the docsDo remember, I assumed the key id will always be pressent, if this is not the case, you might want to add an extra check to the callback function:
function sortById($a,$b)
{
if (!isset($a['id']) && !isset($b['id']))
{
return 0;
}
if (!isset($a['id']))
{
return 1;//move to end of array
}
if (!isset($b['id']))
{
return -1;
}
if ($a['id'] === $b['id'])
{
return 0;
}
return ($a['id'] > $b['id'] ? 1 : -1);
}
This function needn't be that verbose, but since I get the impression you haven't tried that much, I'll leave that up to you.

Sort array by two object properties using anonymous function

I have the following array:
Array
(
[0] => stdClass Object
(
[timestamp] => 1
[id] => 10
)
[1] => stdClass Object
(
[timestamp] => 123
[id] => 1
)
[2] => stdClass Object
(
[timestamp] => 123
[id] => 2
)
)
I'm currently using the following code to sort the array by the timestamp property:
function sort_comments_by_timestamp(&$comments, $prop)
{
usort($comments, function($a, $b) use ($prop) {
return $a->$prop < $b->$prop ? 1 : -1;
});
}
How can I also sort id by id descending when timestamp is the same?
Suggestion is to send in an array with $props
function sort_comments_by_timestamp(&$comments, $props)
{
usort($comments, function($a, $b) use ($props) {
if($a->$props[0] == $b->$props[0])
return $a->$props[1] < $b->$props[1] ? 1 : -1;
return $a->$props[0] < $b->$props[0] ? 1 : -1;
});
}
And then call it with
sort_comments_by_timestamp($unsorted_array,array("timestamp","id"));
If you want it to work with X number of $props you can make a loop inside the usort always comparing a property with its preceding property in the array like this:
function sort_comments_by_timestamp(&$comments, $props)
{
usort($comments, function($a, $b) use ($props) {
for($i = 1; $i < count($props); $i++) {
if($a->$props[$i-1] == $b->$props[$i-1])
return $a->$props[$i] < $b->$props[$i] ? 1 : -1;
}
return $a->$props[0] < $b->$props[0] ? 1 : -1;
});
}
Cheers!
function sort_comments_by_timestamp(&$comments, $prop)
{
usort($comments, function($a, $b) use ($prop) {
if ($a->$prop == $b->$prop)
return $b->id - $a->id;
else
return $a->$prop < $b->$prop ? 1 : -1;
});
}
The above sorts first by the $prop parameter and then secondary by id.
You can do it with ouzo goodies (I know you've heard about it :P).
$result = Arrays::sort($array,
Comparator::compound(
Comparator::compareBy('timestamp'),
Comparator::compareBy('id')
)
);
I know this is fairly old question however didn't find much info for when you want to sort by multiple properties kinda like mySQL ORDERBY does so here's a function from a personal framework
Option 1: order($key, $direction='asc') where $key is a property of the objects and $direction is 'asc' or 'desc'
Option 2: order(array($key => $direction, $key => $direction))
This is similar to option 1 however when 2 objects have the same value for $key, the second sort option from the array passed is used.
public function order($arr, $key=null, $direction='ASC'){
if(!is_string($key) && !is_array($key))
throw new InvalidArgumentException("order() expects the first parameter to be a valid key or array");
$props = array();
if(is_string($key)) {
$props[$key] = strtolower($direction) == 'asc' ? 1 : -1;
}else{
$i = count($key);
foreach($key as $k => $dir){
$props[$k] = strtolower($dir) == 'asc' ? $i : -($i);
$i--;
}
}
usort($arr, function($a, $b) use ($props){
foreach( $props as $key => $val ){
if( $a->$key == $b->$key ) continue;
return $a->$key > $b->$key ? $val : -($val);
}
return 0;
});
return $arr;
}
$result = -1;
if ($a->timestamp < $b->timestamp) {
$result = 1;
} else if ($a->timestamp === $b->timestamp) {
if ($a->id < $b->id) $result = 1;
}
return $result;
Put this within the usort closure. You can also get rid of the $prop argument and the use ($prop) part.
function compare_city($a, $b)
{
// sort by state
$retval = strnatcmp($a->state, $b->state);
// if identical, sort by city
if(!$retval) $retval = strnatcmp($a->city, $b->city);
return $retval;
}
// sort alphabetically by state and city
usort($sortable, __NAMESPACE__ . '\compare_city');
This function worked with me! Answer found at: Sorting SimpleXMLElement Object arrays
The accepted answer has a loop variant that will not always work.
if($a->$props[$i-1] == $b->$props[$i-1])
return $a->$props[$i] < $b->$props[$i] ? 1 : -1;
obj1 prop1:foo, prop2: bar, prop3: test
obj2 prop1:foo, prop2: bar, prop3: apple
At $i = 1, the first comparison is true, and as bar is not less than bar, it will return -1 rather than continue with the loop to evaluate prop3.
Just an FYI.
I have a variant that also takes direction, so each property can also be sorted by ascending or descending order using -1 and 1, respectively.
function sortByProps(&$anArray, $props) {
usort($anArray, function($a, $b) use ($props) {
for($i = 0; $i < count($props); $i++) {
if( $a->{$props[$i][0]} < $b->{$props[$i][0]}) {
return $props[$i][1];
}
else if( $a->{$props[$i][0]} > $b->{$props[$i][0]}) {
return -1* $props[$i][1];
}
}
return 0;
});
}
sortByProps($someArray,(array(['name',-1],['address',-1],['occupation',-1],['favorite_food',-1])));
Here's how to sort by an arbitrary number of criteria, one after the other, breaking ties. It works a lot like an sql order by col1, col2 clause.
I always forget if $a - $b vs $b - $a sorts ascending or descending. Adjust as needed.
$comparatorSequence = array(
function($a, $b) { return $a->timestamp - $b->timestamp; }
, function($a, $b) { return $a->id - $b->id; }
);
usort($theArray, function($a, $b) use ($comparatorSequence) {
foreach ($comparatorSequence as $cmpFn) {
$diff = call_user_func($cmpFn, $a, $b);
if ($diff !== 0) {
return $diff;
}
}
return 0;
});

Checking if array is multidimensional or not?

What is the most efficient way to check if an array is a flat array
of primitive values or if it is a multidimensional array?
Is there any way to do this without actually looping through an
array and running is_array() on each of its elements?
Use count() twice; one time in default mode and one time in recursive mode. If the values match, the array is not multidimensional, as a multidimensional array would have a higher recursive count.
if (count($array) == count($array, COUNT_RECURSIVE))
{
echo 'array is not multidimensional';
}
else
{
echo 'array is multidimensional';
}
This option second value mode was added in PHP 4.2.0. From the PHP Docs:
If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. count() does not detect infinite recursion.
However this method does not detect array(array()).
The short answer is no you can't do it without at least looping implicitly if the 'second dimension' could be anywhere. If it has to be in the first item, you'd just do
is_array($arr[0]);
But, the most efficient general way I could find is to use a foreach loop on the array, shortcircuiting whenever a hit is found (at least the implicit loop is better than the straight for()):
$ more multi.php
<?php
$a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
$b = array(1 => 'a',2 => 'b');
$c = array(1 => 'a',2 => 'b','foo' => array(1,array(2)));
function is_multi($a) {
$rv = array_filter($a,'is_array');
if(count($rv)>0) return true;
return false;
}
function is_multi2($a) {
foreach ($a as $v) {
if (is_array($v)) return true;
}
return false;
}
function is_multi3($a) {
$c = count($a);
for ($i=0;$i<$c;$i++) {
if (is_array($a[$i])) return true;
}
return false;
}
$iters = 500000;
$time = microtime(true);
for ($i = 0; $i < $iters; $i++) {
is_multi($a);
is_multi($b);
is_multi($c);
}
$end = microtime(true);
echo "is_multi took ".($end-$time)." seconds in $iters times\n";
$time = microtime(true);
for ($i = 0; $i < $iters; $i++) {
is_multi2($a);
is_multi2($b);
is_multi2($c);
}
$end = microtime(true);
echo "is_multi2 took ".($end-$time)." seconds in $iters times\n";
$time = microtime(true);
for ($i = 0; $i < $iters; $i++) {
is_multi3($a);
is_multi3($b);
is_multi3($c);
}
$end = microtime(true);
echo "is_multi3 took ".($end-$time)." seconds in $iters times\n";
?>
$ php multi.php
is_multi took 7.53565130424 seconds in 500000 times
is_multi2 took 4.56964588165 seconds in 500000 times
is_multi3 took 9.01706600189 seconds in 500000 times
Implicit looping, but we can't shortcircuit as soon as a match is found...
$ more multi.php
<?php
$a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
$b = array(1 => 'a',2 => 'b');
function is_multi($a) {
$rv = array_filter($a,'is_array');
if(count($rv)>0) return true;
return false;
}
var_dump(is_multi($a));
var_dump(is_multi($b));
?>
$ php multi.php
bool(true)
bool(false)
For PHP 4.2.0 or newer:
function is_multi($array) {
return (count($array) != count($array, 1));
}
I think this is the most straight forward way and it's state-of-the-art:
function is_multidimensional(array $array) {
return count($array) !== count($array, COUNT_RECURSIVE);
}
After PHP 7 you could simply do:
public function is_multi(array $array):bool
{
return is_array($array[array_key_first($array)]);
}
You could look check is_array() on the first element, under the assumption that if the first element of an array is an array, then the rest of them are too.
I think you will find that this function is the simplest, most efficient, and fastest way.
function isMultiArray($a){
foreach($a as $v) if(is_array($v)) return TRUE;
return FALSE;
}
You can test it like this:
$a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
$b = array(1 => 'a',2 => 'b');
echo isMultiArray($a) ? 'is multi':'is not multi';
echo '<br />';
echo isMultiArray($b) ? 'is multi':'is not multi';
Don't use COUNT_RECURSIVE
click this site for know why
use rsort and then use isset
function is_multi_array( $arr ) {
rsort( $arr );
return isset( $arr[0] ) && is_array( $arr[0] );
}
//Usage
var_dump( is_multi_array( $some_array ) );
Even this works
is_array(current($array));
If false its a single dimension array if true its a multi dimension array.
current will give you the first element of your array and check if the first element is an array or not by is_array function.
You can also do a simple check like this:
$array = array('yo'=>'dream', 'mydear'=> array('anotherYo'=>'dream'));
$array1 = array('yo'=>'dream', 'mydear'=> 'not_array');
function is_multi_dimensional($array){
$flag = 0;
while(list($k,$value)=each($array)){
if(is_array($value))
$flag = 1;
}
return $flag;
}
echo is_multi_dimensional($array); // returns 1
echo is_multi_dimensional($array1); // returns 0
I think this one is classy (props to another user I don't know his username):
static public function isMulti($array)
{
$result = array_unique(array_map("gettype",$array));
return count($result) == 1 && array_shift($result) == "array";
}
In my case. I stuck in vary strange condition.
1st case = array("data"=> "name");
2nd case = array("data"=> array("name"=>"username","fname"=>"fname"));
But if data has array instead of value then sizeof() or count() function not work for this condition. Then i create custom function to check.
If first index of array have value then it return "only value"
But if index have array instead of value then it return "has array"
I use this way
function is_multi($a) {
foreach ($a as $v) {
if (is_array($v))
{
return "has array";
break;
}
break;
}
return 'only value';
}
Special thanks to Vinko Vrsalovic
Its as simple as
$isMulti = !empty(array_filter($array, function($e) {
return is_array($e);
}));
This function will return int number of array dimensions (stolen from here).
function countdim($array)
{
if (is_array(reset($array)))
$return = countdim(reset($array)) + 1;
else
$return = 1;
return $return;
}
Try as follows
if (count($arrayList) != count($arrayList, COUNT_RECURSIVE))
{
echo 'arrayList is multidimensional';
}else{
echo 'arrayList is no multidimensional';
}
$is_multi_array = array_reduce(array_keys($arr), function ($carry, $key) use ($arr) { return $carry && is_array($arr[$key]); }, true);
Here is a nice one liner. It iterates over every key to check if the value at that key is an array. This will ensure true

Categories