Find All Second Level Keys in Multi-Dimensional Array in PHP

Find all second level keys in multi-dimensional array in php

<?php

// Gets a list of all the 2nd-level keys in the array
function getL2Keys($array)
{
$result = array();
foreach($array as $sub) {
$result = array_merge($result, $sub);
}
return array_keys($result);
}

?>

edit: removed superfluous array_reverse() function

Find second level array keys only

Not sure I follow all of that, but this seems like it would work instead of all of those foreachs:

$result = odbc_exec($idconex,$consulta) or die("Query failed(-1)");

while($row = odbc_fetch_array($result))
{
$rawdata[] = $row;
}
$myArray = array_keys($rawdata[0]);

In php how to get all keys of multidimensional array with unknown depth

function check($configurations, $count = 0, $levelKeys = array())
{
foreach ($configurations as $k => $configuration) {
if (!array_key_exists($count, $levelKeys) ||
!in_array($k, $levelKeys[$count], true)) {
$levelKeys[$count][] = $k;
}
if (!empty($configuration)) {
$levelKeys = $this->check($configuration, $count + 1, $levelKeys);
}
}
return $levelKeys;
}

DEMO

During the first round I fill the $levelKeys variable with the first key.

In the first IF I use in_array() and array_key_exist() to avoid duplicate values.

If the array I'm analyzing has a nested array I call the check() function passing: nested array, level I have reached, levelKeys.

How to get all the key in multi-dimensional array in php

Try this

function array_keys_multi(array $array)
{
$keys = array();

foreach ($array as $key => $value) {
$keys[] = $key;

if (is_array($value)) {
$keys = array_merge($keys, array_keys_multi($value));
}
}

return $keys;
}

How to sum the 2nd level values of a 2-dim array based on the 2nd level keys?

Just use a foreach or for loop. That is how you work with arrays, be it one, two, three, four, or two-hundred dimensional. Since you will have more than one sum, make the sums variable an array. Declare it outside your loop because you will need it when the loop is complete.

$sums = array();
foreach($multi_dime as $vals)
{
foreach($vals as $key=>$val)
{
// Now we are looping through all values in the second dimension.
// If the key is not in the sums array, make it.
if(!isset($sums[$key])) $sums[$key]=0;
// We add this sum to the key now.
$sums[$key]+= $val;
}
}
ksort($sums); // Just in case you want the keys in order.
print_r($sums); // See all the sums.

How to extract all 2nd level values (leaf nodes) from a 2-dimensional array and join with commas?

With one line, no loop.

echo implode(',', call_user_func_array('array_merge', $data));

Try it here.

how to get all values from an multi dimensional array

Something like this should do it:

$result = [];
array_walk_recursive($input, function($value, $key) use(&$result) {
if ($key === 'id') {
$result[] = $value;
}
});

Check if associative multi-dimensional arrays are equal regardless of order of keys in either level

Here's a recursive comparison function

function CompareRecursive($array1, $array2, &$mismatches) {
foreach ($array1 as $key => $value) {
if (!isset($array2[$key])) {
$mismatches[$key] = [ $value ];
continue;
}

$value2 = $array2[$key];
if (!is_array($value) || !is_array($value2)) {
if ($value != $value2) {
$mismatches[$key] = [
$value, $value2
];
}
} else {
$mismatches_internal = [];
CompareRecursive($value, $value2, $mismatches_internal);
if (!empty($mismatches_internal)) {
$mismatches[$key] = $mismatches_internal;
}
}
}
return empty($mismatches);
}

As an added bonus this keeps track of mismatched entries too, there's a drawback when using this method that it won't work if $array2 has extra elements that $array1 doesn't but you can resolve this by doing:

$isEqual = CompareRecursive($array1,$array2) && CompareRecursive($array2,$array1);

php checking if value exist in a 2 level multidimensional array

Use below code, it will find key to n-level depth and search for given key

function multiKeyExists(array $arr, $key) {

// is in base array?
if (array_key_exists($key, $arr)) {
return $arr[$key]['cat_id']; // returned cat_id
}

// check arrays contained in this array
foreach ($arr as $element) {
if (is_array($element)) {
if (multiKeyExists($element, $key)) {
return $element[$key]['cat_id']; // returned cat_id
}
}

}

return false;
}

How to get the keys of each level of a multidimensional array in PHP

$ini_config['aaa']['email']['main'] = 'me@name.com';
$ini_config['bbb']['email']['ccc'] = 'you@name.com';
$ini_config['bbb']['phone']['local'] = '800-555-1212';
$ini_config['bbb']['phone']['skype'] = '744-222-1234';
$ini_config['ccc']['phone']['main'] = 'domain.com';
$ini_config['ccc']['domain']['https'] = 'https://www. domain.com';
$ini_config['ccc']['fax'] = '744-222-1237';

function keyPaths(array $array, array $carry = [], string $separator = ''): array {
foreach ($array as $key => $value) {
if (is_array($value)) {
$carry = keyPaths($value, $carry, $separator . $key . '_');
} else {
$carry[] = $separator . $key;
}
}
return $carry;
}

$result = keyPaths($ini_config);


Related Topics



Leave a reply



Submit