PHP Multidimensional Array Searching (Find key by specific value)

Another poossible solution is based on the array_search() function. You need to use PHP 5.5.0 or higher. Example $userdb=Array ( (0) => Array ( (uid) => ‘100’, (name) => ‘Sandra Shush’, (url) => ‘urlof100’ ), (1) => Array ( (uid) => ‘5465’, (name) => ‘Stefanie Mcmohn’, (pic_square) => ‘urlof100’ ), (2) => Array ( (uid) … Read more

PHP best way to MD5 multi-dimensional array?

(Copy-n-paste-able function at the bottom) As mentioned prior, the following will work. md5(serialize($array)); However, it’s worth noting that (ironically) json_encode performs noticeably faster: md5(json_encode($array)); In fact, the speed increase is two-fold here as (1) json_encode alone performs faster than serialize, and (2) json_encode produces a smaller string and therefore less for md5 to handle. Edit: … Read more

How to sum all column values in multi-dimensional array?

You can use array_walk_recursive() to get a general-case solution for your problem (the one when each inner array can possibly have unique keys). $final = array(); array_walk_recursive($input, function($item, $key) use (&$final){ $final[$key] = isset($final[$key]) ? $item + $final[$key] : $item; }); Example with array_walk_recursive() for the general case Also, since PHP 5.5 you can use … Read more

Checking if array is multidimensional or not?

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 … Read more

How to create multidimensional array

var numeric = [ [‘input1′,’input2’], [‘input3′,’input4’] ]; numeric[0][0] == ‘input1’; numeric[0][1] == ‘input2’; numeric[1][0] == ‘input3’; numeric[1][1] == ‘input4’; var obj = { ‘row1’ : { ‘key1’ : ‘input1’, ‘key2’ : ‘input2’ }, ‘row2’ : { ‘key3’ : ‘input3’, ‘key4’ : ‘input4’ } }; obj.row1.key1 == ‘input1’; obj.row1.key2 == ‘input2’; obj.row2.key1 == ‘input3’; obj.row2.key2 == … Read more