1

I have a multi-dimension array like:

$fields = 
    Array (
        [1] => Array
            (
                [field_special_features5_value] => Special Function 5
            )

        [2] => Array
            (
                [field_special_features6_value] => Special Function 6
            )

        [3] => Array
            (
                [field_opticalzoom_value] => Optical Zoom
            )
    )

I want to get the value by key, I tried the code below but not work

$tmp = array_search('field_special_features5_value' , $fields);
echo $tmp;

How can I get the value Special Function 5 of the key field_special_features5_value?

Thanks

5 Answers 5

3

print $fields[1]['field_special_features5_value'];

or if you don't know at which index your array is, something like this:

function GetKey($key, $search)
{
    foreach ($search as $array)
    {
        if (array_key_exists($key, $array))
        {
            return $array[$key];
        }
    }

    return false;
}

$tmp = GetKey('field_special_features5_value' , $fields);
echo $tmp;
Sign up to request clarification or add additional context in comments.

Comments

1

If you know where it is located in the $fields array, try :

$value = $fields[1]['field_special_features5_value'];

If not, try :

function getSubkey($key,$inArray)
{
    for ($fields as $field)
    {
         $keys = array_keys($field);
         if (isset($keys[$key])) return $keys[$key];
    }

    return NULL;
}

And use it like this :

<?php
     $value = getSubkey("field_special_features5_value",$fields);
?>

Comments

1

You need to search recursive:

function array_search_recursive(array $array, $key) {
    foreach ($array as $k => $v) {
        if (is_array($v)) {
            if($found = array_search_recursive($v, $key)){
                return $found;
            }
        } elseif ($k == $key) {
             return $v;
        } else {
             return false;
        }
    }
}

$result = array_search_recursive($fields, 'field_special_features5_value');

Comments

0

Your problem is that you have a top-level index first before you can search you array. So to access that value you need to do this:

$tmp = $fields[1]['field_special_features5_value'];

Comments

0

You can do it with recursive function like this

<?php

function multi_array_key_exists($needle, $haystack) {
       foreach ($haystack as $key=>$value) {
         if ($needle===$key) {
           return $key;
         }
         if (is_array($value)) {
           if(multi_array_key_exists($needle, $value)) {
             return multi_array_key_exists($needle, $value);
           }
         }
       }
   return false;
 }
?>

1 Comment

Well, this could work... but why use recursion when in fact the original post implies that the OP knows that the array is NOT actually representing a tree-like structure with an unknown number of 'branches', but a 2-dimensional one...?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.