Ahmed Fouad
Ahmed Fouad

Reputation: 3073

Merge data from qualifying rows of a 2d array into a flat associative array

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,
]

Upvotes: 0

Views: 132

Answers (5)

mickmackusa
mickmackusa

Reputation: 48031

To filter your 2d session array by first level prefix and reduce the payload to a flat associative array (assuming no key collisions are possible), inside your loop's condition block, use the array union assignment operator (+=). This will push each row's associative element into the result array without creating unwanted depth. Even if your rows have more than one element, the result array will still be flat.

Code: (Demo)

$_SESSION = [
    'junk' => ['foo' => 'bar'],
    'saved_query_1' => ['product_category' => 'for-women'],
    'saved_query_2' => ['brand' => '7-diamonds'],
    'junk2' => ['not valuable' => 'rubbish'],
    'saved_query_number' => ['size' => 12],
    'saved_query_11' => ['color' => 882536],
];

$saved = [];
foreach($_SESSION as $k => $v) {
    if (str_starts_with($k, 'saved_query_')) {
        $saved += $v;
    }
}
var_export($saved);

Functional-style code can be used, but will be less performant. Demo

var_export(
    array_merge(
        ...array_values(
            array_filter(
                $_SESSION,
                fn($k) => str_starts_with($k, 'saved_query_'),
                ARRAY_FILTER_USE_KEY
            )
        )
    )
);

Output:

array (
  'product_category' => 'for-women',
  'brand' => '7-diamonds',
  'size' => 12,
  'color' => 882536,
)

Upvotes: 0

Dan Barzilay
Dan Barzilay

Reputation: 4993

Use array_merge_recursive() :

$result = array_merge_recursive($ar1, $ar2 [, array $...]);

Example: http://codepad.viper-7.com/Yr0LTb

Upvotes: 2

Baba
Baba

Reputation: 95161

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 )

See Demo

* ----- 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);

Upvotes: 2

Simon
Simon

Reputation: 174

You should have a look at the array_merge() function in PHP: http://php.net/manual/en/function.array-merge.php

Simply use as follows:

$array1 = Array ( [product_category] => for-women );
$array2 = Array ( [brand] => 7-diamonds );
$array3 = Array ( [size] => 12 );
$array4 = Array ( [color] => 882536 );

$combined = array_merge($array1, $array2, $array3, $array4);

Upvotes: 0

xdazz
xdazz

Reputation: 160923

Use array_merge instead.

$ret = array_merge($arr1, $arr2, $arr3);

With your code, you should do:

$saved = array_merge($saved, $v);

Upvotes: 1

Related Questions