Is copying 2D arrays with “memcpy” technically undefined behaviour?

std::memcpy(arr_copy, arr, sizeof arr); (your example) is well-defined. std::memcpy(arr_copy, arr[0], sizeof arr);, on the other hand, causes undefined behavior (at least in C++; not entirely sure about C). Multidimensional arrays are 1D arrays of arrays. As far as I know, they don’t get much (if any) special treatment compared to true 1D arrays (i.e. arrays … Read more

PHP add elements to multidimensional array with array_push

if you want to add the data in the increment order inside your associative array you can do this: $newdata = array ( ‘wpseo_title’ => ‘test’, ‘wpseo_desc’ => ‘test’, ‘wpseo_metakey’ => ‘test’ ); // for recipe $md_array[“recipe_type”][] = $newdata; //for cuisine $md_array[“cuisine”][] = $newdata; this will get added to the recipe or cuisine depending on … Read more

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

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; }

Sorting a php array of arrays by custom order

You can use usort() to dictate precisely how the array is to be sorted. In this case, the $order array can be used within the comparison function. The example below uses a closure to make life easier. $order = array(3452342, 5867867, 7867867, 1231233); $array = array( array(‘id’ => 7867867, ‘title’ => ‘Some Title’), array(‘id’ => … Read more

“Array to string conversion error” when calling array_diff_assoc() with a multidimensional array

According to it: php -r ‘array_diff(array(“a” => array(“b” => 4)), array(1));’ PHP Notice: Array to string conversion in Command line code on line 1 PHP Stack trace: PHP 1. {main}() Command line code:0 PHP 2. array_diff() Command line code:1 One of your arrays is multidimensional. array_diff only checks one dimension of a n-dimensional array. Of … Read more