1

I am checking does specific value exist as key in array and I need to do following - if it exist to collect some data. Here is a situation:

$valueCode;  //(it can be EV000005, EV000010, etc.)
$temp = array_key_exists_custom($valueCode,$v); //does valueCode exist in array $v

//If it exist I need to collect it is value deeper from structure 
//For example, for EV000005 -> I want to get 'Thread holding'

if ($temp) {
$colValue; // after perfroming code, $colValue should have value  'Thread holding'
}

In case that I have only EV000005 in structure I will do this and get the value.

$colValue = $v[0][$valueCode][0][0][1];

But it is not solution to me because there is situation when my value will not be in $v[0], f.e. when we search EV000010 it will be in $v[1][$valueCode][0][0][1];. I have no idea what is the best way to get this value.

Array example:

    Array
(
    [0] => Array
        (
            [EV000005] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => EN
                                    [1] => Thread holding
                                )

                            [1] => Array
                                (
                                    [0] => nl-NL
                                    [1] => Schroefdraadhouder
                                )

                        )

                )

        )

    [1] => Array
        (
            [EV000010] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => EN
                                    [1] => Desoldering iron
                                )

                            [1] => Array
                                (
                                    [0] => nl-NL
                                    [1] => Desoldeerbout
                                )

                        )

Function for search in array

function array_key_exists_custom($needle, $haystack){
            $result = array_key_exists($needle, $haystack);
            if ($result) {
                return $result;
            }

            foreach ($haystack as $v) {
                if (is_array($v)) {
                    $result = array_key_exists_custom($needle, $v);
                }
                if ($result) {
                    return $result;
                }

            }
            return $result;
        }
5
  • is that part - [0][0][1] - always the same? Commented Dec 29, 2017 at 9:47
  • yes...it is the same. I always need to take this value. Commented Dec 29, 2017 at 9:50
  • change array_key_exists_custom to return value of key found but not boolean. It's very easy. And take $res = array_key_exists_custom($key, $arr)[0][0][1]; Commented Dec 29, 2017 at 9:53
  • What do you mean by 'value of key found'? Value of key is that what I used for search and I am a bit confused... Question edited (added example of my function for search in array). Commented Dec 29, 2017 at 10:17
  • change $result = array_key_exists($needle, $haystack); if ($result) { return $haystack[$needle]; } Commented Dec 29, 2017 at 10:21

3 Answers 3

1

change array_key_exists_custom to return value, which found key points to, in such way:

$result = array_key_exists($needle, $haystack); 
if ($result) { return $haystack[$needle]; }

And get the result by

$res = array_key_exists_custom($key, $arr)[0][0][1];
Sign up to request clarification or add additional context in comments.

Comments

0

I am struggling a bit with your description of the issue - what I infer is that you want to find data below a named key (e.g. EV000005) but you don't know where this key is in the top level array.

This looks like an XY problem. The difficulty arises because the keys you are interested in are not the the keys of the top level array. If your initial data structure was....

    Array (
        [EV000005] => Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => EN
                                [1] => Thread holding
                            )
                        [1] => Array
                            (
                                [0] => nl-NL
                                [1] => Schroefdraadhouder
                            )
                    )
            )
        [EV000010] => Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => EN
                                [1] => Desoldering iron
                            )
                        [1] => Array
                            (
                                [0] => nl-NL
                                [1] => Desoldeerbout
                            )
                    )

...then we wouldn't be having this conversation. You would just use

$colValue = $v['EV000005'][0][0][1];

Based on the (admittedly scarce) information you have provided, there is no reason for the first level of single element arrays, and it's making your life a lot more difficult.

Looking at the deeper data, this anti-pattern repeats, and your data should more correctly be:

         Array (
        [EV000005] => Array
            (
                [EN] => Thread holding
                [nl-NL] => Schroefdraadhouder
            )
        [EV000010] => Array
            (
                [EN] => Desoldering iron
                [nl-NL] => Desoldeerbout
            )

A further issue with your code is that you are performing data filtering operations in PHP. The data did not originate in PHP - it should be stored in a database. You should be doing your filtering and sorting in the database, not in PHP.

Assuming for now, that there is a very, very good reason that you can't fix the problem properly and the only solution is to apply your approach....

 function retrieve_description($item, $lang, &$arr)
 {
     foreach ($arr as $record) {
        if (is_array($record[$item])) {
           foreach ($record[$item] as $description) {
              if ($description[0]==$lang) return $description[1];
           }
           return false;
        }
     }
     return false;
 }

Stop assigning random numbers to things which already have identifiers.

Comments

-1

Use function is_array() for each elements of array to go "deeper"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.