A
A
Allarion2017-06-01 23:41:44
PHP
Allarion, 2017-06-01 23:41:44

How to find a value in a multidimensional associative array and display an array of data by php keys?

Good afternoon! There is a multidimensional associative array with data with different levels of nesting. Roughly like this:

Array
(
    [0] => Array
        (
            [name1] => FRUITS
            [name2] => 50
            [name3] => SHTUKA
            [name4] => 90
            [name5] => RUR
            [name6] => Array
                (
                    [0] => Array
                        (
                            [name1] => APPLE
                            [name2] => 30
                            [name3] => SHTUKA
                            [name4] => 80
                            [name5] => RUR
                            [name6] => Array
                                (
                                    [0] => Array
                                        (
                                            [name1] => ANTONOVKA
                                            [name2] => 20
                                            [name3] => SHTUKA
                                            [name4] => 70
                                            [name5] => RUR
                                            [name6] => 
                                            [name7] => 
                                        )

                                    [1] => Array
                                        (
                                            [name1] => ZIMNIE
                                            [name2] => 10
                                            [name3] => SHTUKA
                                            [name4] => 65
                                            [name5] => RUR
                                            [name6] => 
                                            [name7] => 
                                        )

                                )
                
                            [name7] => 
                        )
            
                    [name7] => 
                )

How can you make a function that would recursively go through the entire array, looking for a specific value, for example ANTONOVKA, and finding it would write data from certain keys to $result , getting something like the following at the output:
$result = array(
  [0] => ANTONOVKA
    [1] => 20
    [2] => SHTUKA
    [3] => 70
    [4] => RUR
)

As I understand it, the input should be: $ar - the large array itself, which is searched for $findValue - the Value to be found (in the ANTONOVKA example) $executeKeys - an array with a list of keys $executeKeys = array ('name1','name2', 'name3','name4','name5') I solved my own question, maybe it will be useful for someone:
function findArray ($ar, $findValue, $executeKeys)
function findArray ($ar, $findValue, $executeKeys){
  $result = array();

  foreach ($ar as $k => $v) {
    if (is_array($ar[$k])) {
      $second_result = findArray ($ar[$k], $findValue, $executeKeys);
      $result = array_merge($result, $second_result);
      continue;
    }
    if ($v === $findValue) {
      foreach ($executeKeys as $val){
        $result[] = $ar[$val];
      }
      
    }
  }
  return $result;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Allarion, 2017-06-03
@Allarion

Decision

function findArray ($ar, $findValue, $executeKeys){
  $result = array();

  foreach ($ar as $k => $v) {
    if (is_array($ar[$k])) {
      $second_result = findArray ($ar[$k], $findValue, $executeKeys);
      $result = array_merge($result, $second_result);
      continue;
    }
    if ($v === $findValue) {
      foreach ($executeKeys as $val){
        $result[] = $ar[$val];
      }
      
    }
  }
  return $result;
}

B
Boris Korobkov, 2017-06-02
@BorisKorobkov

There is one correct word in the text - "recursively". So write a recursive function. The input is an array, the output is a new array, inside foreach and, if necessary, RECURSION.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question