Reputation: 8070
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?
Upvotes: 0
Views: 2564
Reputation: 12689
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.
Upvotes: 1
Reputation: 4369
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>
Upvotes: 1
Reputation: 500
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>
Upvotes: 0