I have an array and it looks like this:

Array ( [0] => Array ( [0] => Array ( [ID] => 884 [UF_FIRSTNAME] => ТЕСТ [UF_MIDDLENAME] => ТЕСТ [UF_LASTNAME] => ТЕСТ ) [1] => Array ( [ID] => 885 [UF_FIRSTNAME] => ТЕСТ [UF_MIDDLENAME] => ТЕСТ [UF_LASTNAME] => ТЕСТ ) ) ) 

You need to bring the array into such a one-dimensional view:

 Array ( [ID] => 884 [UF_FIRSTNAME] => ТЕСТ [UF_MIDDLENAME] => ТЕСТ [UF_LASTNAME] => ТЕСТ [ID] => 885 [UF_FIRSTNAME] => ТЕСТ [UF_MIDDLENAME] => ТЕСТ [UF_LASTNAME] => ТЕСТ ) 

Tell me how to do this? array_walk_recursive doesn’t suit me because it puts the keys in order and the source keys are lost.

  • 7
    What you want is not possible. You get an array with duplicate keys. I wonder what value you expect in response to such a call: array["ID"] - Oleg
  • @Oleg can I then make at least as indices the same for arrays: Array( [0] => (Array[0]=>() Array[0]=>()...)) - Evgeny Pivovarov
  • Identical indices within a single array are not possible in principle. Array( [0] => (Array[0]=>() Array[1]=>()...)) That's it, yes - Oleg
  • I need to mix it all into one mess, so that a sample of $ array [0] ["UF_FIRSTNAME"] would turn out to have all the key values ​​of UF_FIRSTNAME ... but I didn’t go to that steppeYevgeny Pivovarov

4 answers 4

I need to mix it all up into one mess, so that I get a sample of $array[0]["UF_FIRSTNAME"] get all the key values ​​of UF_FIRSTNAME ...

this will help you

 $result = array_column($array[0], 'UF_FIRSTNAME'); 

    If correctly understood from the comment, then the task, when accessing the key, is to derive all its possible values ​​from the multidimensional array.

    If so, you can stir up this:

      $newArray = []; $commonArray = Array ( [0] => Array ( [0] => Array ( [ID] => 884 [UF_FIRSTNAME] => ТЕСТ [UF_MIDDLENAME] => ТЕСТ [UF_LASTNAME] => ТЕСТ ) [1] => Array ( [ID] => 885 [UF_FIRSTNAME] => ТЕСТ [UF_MIDDLENAME] => ТЕСТ [UF_LASTNAME] => ТЕСТ ) ) ) foreach ($commonArray as $values) { if (!empty($values)) { foreach ($values as $key => $value) { $newArray[$key][] = $value; } } } 

    and further we have the following structure

     $newArray = [ 'ID' => [values...], 'UF_FIRSTNAME' => [values...], 'UF_MIDDLENAME' => [values...], 'UF_LASTNAME' => [values...], ] 

    and how to deduce this is the next question:

    array_unique - if we want to leave only unique values

    implode - if we want to output a string with a separator, for example

     echo implode(', ', $newArray['UF_FIRSTNAME']); //все значения из ключей UF_FIRSTNAME 

    If you do not use array_unique, then when iterating over the keys, the internal value pointers will correspond as if they were in 1 array. for example

     foreach ($newArray['ID'] as $key => $value) { echo 'ID is ' . $value; echo 'UF_FIRSTNAME for ID ' . $value . ' is ' . $newArray['UF_FIRSTNAME'][$key]; // etc } 

    I hope to guess)

      Probably you need not what you are asking. If you explain the real problem, they can help you.

      In the meantime, I have a fun solution to this problem (just for fun):

       <?php /** * Разворачивает многомерный массив в одномерный, нумеруя ключи */ function aflatten(array $arr, string $keyIdSeparator = ''): array { $result = []; $increments = []; array_walk_recursive($arr, function($item, $key) use($keyIdSeparator, &$result, &$increments) { if ( ! isset($increments[$key])) { $increments[$key] = 0; } $newKey = $key . $keyIdSeparator . $increments[$key]; $result[$newKey] = $item; $increments[$key]++; }); return $result; } $arr = [ [ [ 'ID' => 884, 'UF_FIRSTNAME' => 'ТЕСТ', 'UF_MIDDLENAME' => 'ТЕСТ', 'UF_LASTNAME' => 'ТЕСТ', ], [ 'ID' => 885, 'UF_FIRSTNAME' => 'ТЕСТ', 'UF_MIDDLENAME' => 'ТЕСТ', 'UF_LASTNAME' => 'ТЕСТ', ], ], ]; $keyIdSeparator = '.'; $result = aflatten($arr, $keyIdSeparator); foreach ($result as $key => $value) { $sourceKey = substr($key, 0, strrpos($key, $keyIdSeparator)); echo $sourceKey, ': ', $value, "\n"; } 

        All you can do is combine the values ​​of the subarrays under the same keys

         $array = [ [ 'ID' => 884, 'UF_FIRSTNAME' => 'ТЕСТ', 'UF_MIDDLENAME' => 'ТЕСТ', 'UF_LASTNAME' => 'ТЕСТ' ], [ 'ID' => 885, 'UF_FIRSTNAME' => 'ТЕСТ', 'UF_MIDDLENAME' => 'ТЕСТ', 'UF_LASTNAME' => 'ТЕСТ' ] ]; foreach ($array as $key => $value) { foreach ($value as $k => $v) { $new[$k][] = $v; } } print_r($new); 
        • thanks, the code is working, but with my array it’s why it doesn’t roll (( - Evgeni Pivovarov
        • @ Evgeny Pivovarov, did you specify $array (like mine) or $array[0] in a loop? - Let's say Pie
        • And so and so tried ... - Yevgeny Pivovarov