0

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

5 Answers 5

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Please see edited question. I am missing something. I need the merge /combine to be done when I am looping thru $_SESSION
2

Use array_merge_recursive() :

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

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

1 Comment

Recursion is unnecessary for the asked question.
1

Use array_merge instead.

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

With your code, you should do:

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.