0

I have this php function :

<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(108542)->current_query()->get_array();
echo '<pre>',print_r($sf_current_query,true),'</pre>';
?>

and as a result this multi-level array :

Array
(
    [_sft_areas] => Array
        (
            [name] => Areas
            [singular_name] => Area
            [all_items_label] => All Areas
            [type] => taxonomy
            [active_terms] => Array
                (
                    [0] => Array
                        (
                            [id] => 288
                            [name] => LA Zone
                            [value] => la-zone
                            [count] => 18
                        )

                    [1] => Array
                        (
                            [id] => 291
                            [name] => Bel Air
                            [value] => bel-air
                            [count] => 2
                        )

                )

        )

)

I'm gonna get rid of the print_r but I'm struggling to use a foreach function in order to echo something like this :

Areas : LA Zone, Bel Air.

I'm pretty new to php, can anyone help me with that ?

thanks !

mmdwc
  • 1,095
  • 6
  • 27
  • 53

2 Answers2

0

This should do the trick:

foreach ( $sf_current_query as $results ) {

    //Check whether to use the single or plural label
    $label = count( $results['active_terms'] ) > 1 ? $results['name'] : $results['singular_name'];

    //Collect all names
    $names = array();
    foreach ( $results['active_terms'] as $result ) {
        $names[] = $result['name'];
    }

    //Print label and comma separated list of names
    if ( !empty( $names ) ) printf( '<p>%s: %s</p>', $label, implode( ', ', $names ) );
}

First a foreach loop to cycle through the top level of your multi-dimensional array. Which in this case only contains one record: _sft_areas

After that the label check counts the number of records in the active_terms array using the count() function. And then uses that outcome in combination with the PHP ternary comparison operator to check if the label should be singular or plural.

Then all the names are fetched with another foreach loop and placed in a separate array called $names. (Also perfectly fine to use the array_column() function here.)

And lastly I've printed each label and list of names to a <p> element using the printf() function, but you can use any kind of output you like. The implode() function lets you create a string from an array using a separator. In this case it creates a comma separated list from the $names array.

Terminator-Barbapapa
  • 3,063
  • 2
  • 5
  • 16
0

If you need to loop to get more than name etc. just look at the structure and loop the innermost that you need:

foreach($sf_current_query['_sft_areas']['active_terms'] as $terms) {
    echo $terms['name'];
}

Or just extract the names and implode them:

echo implode(', ', array_column($sf_current_query['_sft_areas']['active_terms'], 'name'));
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87