Foreach Loop Inside Array

Foreach loop inside array

That's invalid syntax. You'd have to build the "parent" portions of the array first. THEN add in the sub-array stuff with the foreach loop:

$foo = array(
'label' => 'Assign to user',
'desc' => 'Choose a user',
'id' => $prefix.'client',
'type' => 'radio',
'options' => array()
);

foreach ($clients as $user) {
$foo['options'][] = array (
'label' => $user->user_login,
'value' => $user->user_login,
);
}

How to use foreach inside array of foreach?

try this code:

public function dropDownField($request)
{
$value = [];
if (key_exists("drop-name", $request)) {
foreach ($request['drop-name'] as $key => $data) {
$options = [];
foreach($request['drop-option-label'] as $key => $data) {
$options[] = [
'label' => ($request['drop-option-label'][$key]) ?($request['drop-option-label'][$key]) : null,
'value' => ($request['drop-option-value'][$key]) ? ($request['drop-option-value'][$key]) : null,
];
}

$value[] = [
"type" => ($request['drop-type'][$key]) ? (($request['drop-type'][$key])) : null,
"label" => ($request['drop-label'][$key]) ? ($request['drop-label'][$key]) : null,
"name" => ($request['drop-name'][$key]) ? ($request['drop-name'][$key]) : null,
"placeholder" => ($request['drop-placeholder'][$key]) ? ($request['drop-placeholder'][$key]) : null,
"options" => $options
];
}
}
return $value;
}

Can I use another loop inside forEach in Javascript? or is it best i use to separate loops?

Can I use another loop inside forEach in Javascript?

Yes! There is nothing wrong with nested loops in Javascript.

How to use the foreach inside array in php?

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.

Changing a value inside the array in a forEach() loop

Suggestion : move newArray outside the clicked function as it is going to update on click.

Implementation : You can use Array.filter() method on newArray to check if record as per the input id is available or not and then by using Array.find() you can do filtering on the mainArray if there is no data in newArray.

Live demo :

const newArray = [];

function clicked(inp){
const mainArray = [
{ id: 1, name: "Shoes",stock: 5, price: 10 },
{ id: 2, name: "Bag",stock: 10, price: 50 },
];

const findInNewArr = newArray.filter(item => inp === item.id);

if (findInNewArr.length) {
newArray[0].stock += 1;
} else {
const findInMainArray = mainArray.find(element => inp === element.id);
if (findInMainArray) {
findInMainArray.stock = 1;
newArray.push(findInMainArray);
}
}

console.log(newArray);

}
<button id="1" onclick="clicked(2)">Click me</button>

How to make array inside in foreach loop - php?

I suppose you're looking for this:

$input = array("teamA","teamB","teamC");
$data = [];
foreach($input as $value){
$assign = "50"; /* The data just temp */
$data[$value] = $assign;
}

echo $data["teamA"];

If $assign is same for all keys:

$data = array_fill_keys($input, 50);

php - How to insert a foreach loop inside a multidimensional array

I want say thank you to @adrianRosi for the help and the input he gives me.

In the end I find my solution, that it's json_encode the array before add into $data in json format.

in this way:

            $product_list = [];    

foreach ($order->pad_products as $product) {
$product_list[] = array(
"id" => "$id",
"..." => "...",
);
};

$data_products['lines'] = $product_list;
$json_products = json_encode($data_products);
$json_products_edit = substr($json_products, 1, -1); // to delete the {}

$prezzo_totale = $order->pad_price_total;

$json_data = '{
...
...
'.$json_products_edit.'
}';


Related Topics



Leave a reply



Submit