Question

How can I print out two associative arrays and use a loop to explode the second array values? The problem is explode

array attributes will be limited to 3 values, and values will be limited to 4.

[attributes] => Array ( [0] => Array ( [attribute] => Colour ) [1] => Array ( [attribute] => Size ) )
[values] => Array ( [0] => Array ( [value] => Red,Green,Blue ) [1] => Array ( [value] => Large,Medium,Small ) ) 

If it helps I can save the key for values as the attribute name:

Array ( [colour] => Red,Green,Blue )

The code:

foreach ($attributes as $k => $v)
{
    echo "<b>" .$v['attribute'] ."</b>"."<br>";

    foreach ($values as $val)
    {

        $value = $val['value'];
        $expld = explode(",", $value);
        foreach ($expld as $explval)
        {
            $qryString = array( 'search' => $search,
                                'attr' => $explval
                              );

            echo anchor('products/item_search?'. http_build_query($qryString), $explval) ."<br>";
        }
    }
}
Était-ce utile?

La solution

You don't want to loop over all the $values in the inner loop, you just want to explode the one with the same index as the current attribute and loop over those.

foreach ($attributes as $index => $v) {
    echo "<b>{$v['attribute']}</b><br>";
    foreach (explode(',', $values[$index]['value']) as $explval) {
        $qryString = array( 'search' => $search,
                            'attr' => $explval
                          );
        echo anchor('products/item_search?'. http_build_query($qryString), $explval) ."<br>";
    }
}

Autres conseils

 for ($i = 0; $i<count($attributes); $i++)
  {
    echo "<b>" .$attributes[$i]['attribute'] ."</b>"."<br>";


        $expld = explode(",", $values[$i]['value']);
        foreach ($expld as $explval)
        {
            $qryString = array( 'search' => $search,
                'attr' => $explval
            );

            echo anchor('products/item_search?'. http_build_query($qryString),$explval) ."<br>";
        }

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top