-1

Im struggling with a multidimensionnal array while building a form using ajax and PHP. My ajax send a multidimensionnal array to php. And i want to convert this array to a single dimensionnal one.

so considering this code:

$reasons = $_POST['reasons'];
var_dump($reasons);

which output this:

    array(1) {
      [0]=>
        array(2) {
          ["id"]=>
            string(2) "37"
          ["reason"]=>
            string(9) "Something"
        }
   }

I now want it to be like this:

$reasons = {
  37 => "Something"
}

How can i do that ? I have searched for hours and didn't find the right solution to do it...

What i have tried:

1)

function array_flatten($array) { 

    if (!is_array($array)) {
      return FALSE; 

    }

    $reasons = array(); 

    foreach ($array as $key => $value) { 

      if (is_array($value)) { 

        $reasons = array_merge($reasons, array_flatten($value)); 

      } else { 
        
        $reasons[$key] = $value; 
      
        }

    }

    return $reasons;

}

array_flatten($_POST['reasons']);

var_dump($reasons);

Output: NULL
$reasons = $_POST['reasons'];

$tabReasons = array_reduce($reasons, 'array_merge', array());

var_dump($tabReasons);

Output this:

array(2) {
  ["id"]=>
    string(2) "37"
  ["reason"]=>
    string(9) "Parce que"

}

Which kind of work but my multidimenssional array can store multiple ID and Reasons. And when that happens, the code above only returns me a single pair of key & value

Like if i have two id and two reasons in my multidimenssional, it will only return me last id and reason into a single array...

i want something like : array(id => reason, id => reason, etc...)

The-Evil-Fox
  • 111
  • 1
  • 8
  • 1
    Does this answer your question? [Convert multidimensional array into single array](https://stackoverflow.com/questions/6785355/convert-multidimensional-array-into-single-array) – El_Vanja Feb 23 '21 at 10:21
  • _I have searched for hours and didn't find the right solution to do it_... please post your code, what you have tried? – Alive to die - Anant Feb 23 '21 at 10:24
  • I edited the code with everything i have tried and more explanations about what im trying to do – The-Evil-Fox Feb 23 '21 at 10:44

3 Answers3

0

somthing like this

 function array_flatten($data) { 
         $res=array();
         foreach ($data as $val) { 
            $res[$val['id']] =$val['reason'];
        }
        return $res;
    }
0

You can use array_column() to extract linear data of column, and array_combine() to combine them as keys/values.

$reasons = [
    ['id' => 37, 'reason' => 'something'],
    ['id' => 38, 'reason' => 'something else'],
];
      
$indexed = array_combine(array_column($reasons, 'id'), array_column($reasons, 'reason'));

print_r($indexed);

Output :

Array
(
    [37] => something
    [38] => something else
)
Syscall
  • 19,327
  • 10
  • 37
  • 52
-1

Not sure whether your data are more complicated than shown here or not, but from the data provided it's quite straight forward to do.

$result = [
    $reasons[0]['id'] => $reasons[0]['reason']
];
Anurat Chapanond
  • 2,837
  • 3
  • 18
  • 31