6

I have the following array structure:

Array
(
    [0] => Array
        (
            [product_option_id] => 236
            [option_id] => 14
            [name] => Masura S
            [type] => select
            [option_value] => Array
                (
                    [0] => Array
                        (
                            [product_option_value_id] => 33
                            [option_value_id] => 53
                            [name] => Alb
                            [price] => 
                            [price_prefix] => +
                        )

                    [1] => Array
                        (
                            [product_option_value_id] => 35
                            [option_value_id] => 55
                            [name] => Rosu
                            [price] => 
                            [price_prefix] => +
                        )

                )

            [required] => 0
        )

    [1] => Array
        (
            [product_option_id] => 237
            [option_id] => 15
            [name] => Masura M
            [type] => select
            [option_value] => Array
                (
                    [0] => Array
                        (
                            [product_option_value_id] => 34
                            [option_value_id] => 58
                            [name] => Rosu
                            [price] => 
                            [price_prefix] => +
                        )

                )

            [required] => 0
        )
)

I find myself lost in trying to display all the [name] values from this array.

What I am trying to do is to populate a form with dropdown selects based on the first level [name] (like [name] => Masura S) and then a second dropdown select with the second level [name] (like [name] => Alb).

I would appreciate it if you have any pointers...

2
  • A union query using the similar name field between these two tables, with an alias/custom field indicating level based on table source, should get you a better formed array to work with. Have you tried anything like that? Commented Jun 12, 2011 at 7:35
  • Well Bob, this is beyond my goal, I do not want to modify the Model of this MVC, working with what is provided seems like a better choice for the moment. Commented Jun 12, 2011 at 8:15

4 Answers 4

9

Try this:

$name = array_column($array, 'name');
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect solution
6

You can populate the first select this way:

<select>

    <?php $c=count($array);
    for ( $i=0; $i < $c; $i++)
    { ?>
        <option><?php echo $array[$i]['name'];?></option>
    <?php } ?>   

</select>

2nd select:

<select>

    <?php 
    for ( $i=0; $i < $c; $i++)
    { 
        $c2=count($array[$i]); 
        for ($j=0;$j<$c2;$j++){ 
    ?>
        <option><?php echo $array[$i][$j]['name'];?></option>
    <?php }} ?>

</select>

2 Comments

Thanks Belonwu, this has got me close enough to figure it out, it now works as I want it!
I have made a few modifications to the example Belonwu has provided, and with a little help from a jQuery chaining script I now have the working code. I just had to modify this line: $c2=count($options[$i]['option_value']); to count only the options available to a specific name and than this: <?php echo $options[$i]['option_value'][$j]['name'];?> to display the correct options for that name. Thanks! Now... to sort out the rest of my code :)
5

I'd separate the names to separate arrays like this, after that it should be easy to populate dropdowns as needed:

$product_names = $option_names = array();
foreach ($products as $index => $product) {
    $product_names[$index] = $product['name'];

    foreach ($product['option_value'] as $option) {
        $option_names[$index][] = $option['name'];
    }
}

When you want product name for array index 0, you'd use $product_names[0] (a string) and option names for that product could be found from $option_names[0] (an array).

Code above doesn't care about existing ID's so if you need them for the form, you'd need to expand the code a bit more.

1 Comment

I shall try and see where this gets me :) Thanks!
2

You will need to use a recursive function
here's an example

function recursion($array) {
    foreach ($array as $key => $value) {
        echo $value;
        if (is_array($value))
            $this->recursion($value);
    }
}

recursion($array);

1 Comment

how can I return the value from this function?I want to return the $value.I modified the function in this way function recursion($array) { foreach ($array as $key => $value) { if($key==='sourceChannel'){ $getId = $value['id']; return $getId; } if (is_array($value)) recursion($value); } } But it always returns blank!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.