I have a $_SESSION
array which I filter/sanitize by keys inside of a loop.
This produces a 2d array of qualifying data, but I want to reduce it to a flat associative array.
Theoretical input:
$_SESSION = [
// ...some unwanted row
'saved_query_something' => ['product_category' => 'for-women'],
'saved_query_something_else' => ['brand' => '7-diamonds'],
// ...some unwanted row
'saved_query_blah' => ['size' => 12],
'saved_query_blar' => ['color' => 882536],
];
This is my current code that doesn't correctly reduce the data structure while sanitizing.
foreach ($_SESSION as $k => $v) {
if (strstr($k, 'saved_query_') == true) {
$saved = array_merge($v);
}
}
The desired result:
[
'product_category' => 'for-women',
'brand' => '7-diamonds',
'size' => 12,
'color' => 882536,
]
You can try using array_merge
$array0 = Array ( "product_category" => "for-women" );
$array1 = Array ( "brand" => "7-diamonds" ) ;
$array2 = Array ( "size" => "12" ) ;
$array3 = Array ( "color" => "882536" );
$array = array_merge($array0,$array1,$array2,$array3);
print_r($array);
Output
Array ( [product_category] => for-women [brand] => 7-diamonds [size] => 12 [color] => 882536 )
* ----- Update ----- *
If you are looking through a session
$_SESSION = Array();
$_SESSION[0] = Array("product_category" => "for-women");
$_SESSION[1] = Array("brand" => "7-diamonds");
$_SESSION[2] = Array("size" => "12");
$_SESSION[3] = Array("color" => "882536");
$final = array();
foreach ( $_SESSION as $key => $value ) {
$final = array_merge($final, $value);
}
print_r($final);