Home > Mobile >  Add array to Value
Add array to Value

Time:01-01

After a $single_array = array_reduce ($new_array, 'array_merge_recursive', array ());

I get this result

array (size=9)
  'key1' => string 'value1' 
  'key2' => string 'value2' 
  'key3' => string 'value3' 
  'key7' => 
    array 
      0 => string 'value7'
      1 => string 'value13' 
  'key8' => 
    array 
      0 => string 'value8' 
      1 => string 'value14'
  'key9' => 
    array 
      0 => string 'value9' 
      1 => string 'value15' 
  'key19' => string 'value19' 
  'key20' => string 'value20' 
  'key21' => string 'value21' 

but i would like that

array 
  'key1' =>
    array 
      0 => string 'value1' 
  'key2' =>
    array 
      0 => string 'value2' 
  'key3' =>
    array 
      0 => string 'value3' 
  'key7' => 
    array 
      0 => string 'value7' 
      1 => string 'value13' 
  'key8' => 
    array 
      0 => string 'value8' 
      1 => string 'value14' 
  'key9' => 
    array 
      0 => string 'value9' 
      1 => string 'value15' 
  'key19' =>
    array 
      0 => string 'value19' 
  'key20' =>
    array 
      0 => string 'value20'
  'key21' =>
    array 
      0 => string 'value21'

It's probably very simple, but I've been blocking for a few hours.

In short, I would like my array_merge_recursive not to add a key only to the same values, but to add an array[0] also to those which were not the same.

EDIT

The code at the start, the goal being to simplify it and knowing that the values generated as well as the order of the structure can be random.

<?php
$array = array(
    'sensors' => array(
        '0' => array(
            'data' => array(
                '0' => array(
                    'key1' => 'value1',
                    'key2' => 'value2',
                    'key3' => 'value3',
                )
            )
        ),
        '1' => array(
            'data' => array(
                '0' => array(
                    'key7' => 'value7',
                    'key8' => 'value8',
                    'key9' => 'value9',
                )
            )
        ),

        '2' => array(
            'data' => array(
                '0' => array(
                    'key7' => 'value13',
                    'key8' => 'value14',
                    'key9' => 'value15',
                )
            )
        ),
        '3' => array(
            'data' => array(
                '0' => array(
                    'key19' => 'value19',
                    'key20' => 'value20',
                    'key21' => 'value21',
                )
            )
        )
    )
);
$new_array = array();
foreach ($array as $element1) {
    foreach ($element1 as $j => $element2) {
        foreach ($element2 as $element3) {
            foreach ($element3 as $element4) {
                $new_array[$j] = $element4;
                $single_array = array_reduce($new_array, 'array_merge_recursive', array());
               
            }
        }
    }
}
var_dump($single_array);

CodePudding user response:

You can use array_walk_recursive on the original array:

$result = [];
array_walk_recursive($array, function ($value, $key) use (&$result) {
    $result[$key][] = $value;
});

After running this on the example array you provided, $result will be:

[
  'key1' => ['value1'],
  'key2' => ['value2'],
  'key3' => ['value3'],
  'key7' => ['value7', 'value13'],
  'key8' => ['value8', 'value14'],
  'key9' => ['value9', 'value15'],
  'key19' => ['value19'],
  'key20' => ['value20'],
  'key21' => ['value21']
];

CodePudding user response:

Thank you, I found, I was just confusing in_array and is_array. My bad

$array2 = array();
foreach ($single_array as $key1 => $value1) {
     if (is_array($value1)) {
         foreach($value1 as $key2 => $value2) {
             $array2[$key1][$key2] = $value2;
         }
     }
     else {
         $array2[$key1][] = $value1;
     }
}

var_dump($array2); 
  • Related