0

Whats the simplest way to convert the following nested array into a simple array

array(
    'user' => array(
         'firstName' => 'Test',
         'lastName' => 'Test'
    ),
    'title' => 'Test'
)

Into

array(
    'user.firstName' => 'Test',
    'user.lastName' => 'Test',
    'title' => 'Test'
)

I need this format for a doctrine query where statement.

Alexander Schranz
  • 2,154
  • 2
  • 25
  • 42

1 Answers1

2

try this

$arr=array(
    'user' => array(
         'firstName' => 'Test',
         'lastName' => 'Test'
    ),
    'title' => 'Test'
);
$bigArr=array();
foreach($arr as $arK=>$arV){
    if(is_array($arr[$arK])){
        foreach($arr[$arK] as $k=>$v){
            $bigArr[$arK.".".$k]=$v;
        }
    }
    else{
        $bigArr[$arK]=$arV;
    }
}
var_dump($bigArr);

Output:-

array (size=3)
  'user.firstName' => string 'Test' (length=4)
  'user.lastName' => string 'Test' (length=4)
  'title' => string 'Test' (length=4)
Bushra Shahid
  • 781
  • 7
  • 20