0

I am trying to do foreach inside array. I am trying to generate the select box using array value everything seems ok when i give a value manually.

array(
    'name' => __('Testing Selection', 'test'),
    'id' => 'testing',
    'css' => 'min-width:150px;',
    'std' => '2', 
    'default' => '2',
    'type' => 'select',
    'options' => array(
        '1' => __('Test1', 'test'),
        '2' => __('Test2', 'test'),
        '3' => __('Test3', 'test'),
    ),
),

In the above code options key contains three values like 1, 2, 3 and the above code is working. But i want to loop all product id here using foreach but it seems not working for me may be i am trying a wrong way. I know foreach inside array is invalid that's why i am trying this way.

$foos = array(
    'name' => __('Testing Selection', 'test'),
    'id' => 'testing',
    'css' => 'min-width:150px;',
    'std' => '2', 
    'default' => '2',
    'type' => 'select',
    'options' => array(),
),

After array i did foreach

$args = array('post_type' => 'product', 'posts_per_page' => '-1');
$getproducts = get_posts($args);

foreach ($getproducts as $product) {
    $foos['options'][] = array(
        $product->ID => $product->get_title,
    );
}

I want to list 20 more products in the select box everything manually is hard to me can anyone suggest me to use the foreach inside array?

5
  • What do you want the end result to be? Commented Apr 18, 2014 at 13:10
  • Thanks for your comment :) End result should be list of product id with name in a select box. Commented Apr 18, 2014 at 13:19
  • How do you want the end array to look. Commented Apr 18, 2014 at 13:21
  • Can you please check this link here is the similar method i am trying stackoverflow.com/questions/14446174/foreach-loop-inside-array Commented Apr 18, 2014 at 13:24
  • What is the current end value of $foos and what do you expect? Commented Apr 18, 2014 at 13:54

3 Answers 3

1

With PHP functions like array_map() or array_reduce() you can create the new array inside an array. array_map () is useful for creating the values ​​for an array, but you can not manipulate the keys with it. Because of that we can use array_reduce() to simulate the behavior of array_map() and to create the associative array needed for options.

$foos = array(
    'name'      => 'Testing Selection',
    'id'        => 'testing',
    'css'       => 'min-width:150px;',
    'std'       => '2', 
    'default'   => '2',
    'type'      => 'select',
    'options'   => array_reduce( get_posts( 'post_type=product&posts_per_page=-1' ), function( $result, $item ) { 
        $result[$item->ID] = $item->post_title;
        return $result;
    })
);

If you don't like the approach, you can create new function that will return the required array for options, and thus improve the readability of code.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure exactly what you have in mind, but try this template:

<select>
<?php
    $items = array(
        'one' => 'Item one',
        'two' => 'Item two',
        'three' => 'Item three'
    );
    foreach(array_keys($items) as $item_id) {
        echo "<option name=\"$item_id\">$items[$item_id]</option>\n";
    }
?>
</select>

1 Comment

+1 Thanks for your template :) Can you please check this link here is the similar method i am trying stackoverflow.com/questions/14446174/foreach-loop-inside-array
0

If you want to insert data to database , then you should go with foreach key value combination. Here given the example

<?php
if(isset($_POST['submit'])){
    $myArray = array();
    $myArray = array(
        'name' => $_POST['name'],
        'contact' => $_POST['contact'],
        'address' => $_POST['address']
    );

    foreach($myArray as $key=>$value){
        echo $value;
    }
}
?>
<html>
    <head>
    </head>
    <body>
        <form action="#" method="post">
            name<input type="text" name="name"/>
            contact<input type="text" name="contact"/>
            address<input type="text" name="address"/>
            <input type="submit" name="submit"/> 
        </form>
    </body>
</html>

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.