Foreach array inside array - php

How can I show only name and lastname for mecanicien?
$aMecaniciens = array(
array(
"idMecanicien"=>1,
'vchNomMecanicien'=>"Guérand",
'vchPrenomMecanicien'=>"Bob"
),
array(
"idMecanicien"=>2,
'vchNomMecanicien'=>"Lim",
'vchPrenomMecanicien'=>"Bao"
),
array(
"idMecanicien"=>3,
'vchNomMecanicien'=>"Cadoret",
'vchPrenomMecanicien'=>"Cadoret"
)
);
foreach ($aMecaniciens as $value) {
foreach ($value as $key) {
echo "<option value=\"value\">$key[1].$key[2]</option>";
}
}

You don't need the inner foreach. You can use the named keys of $value to output your options.
foreach ($aMecaniciens as $value) {
echo "<option value=\"$value[idMecanicien]\">
$value[vchNomMecanicien].$value[vchPrenomMecanicien]
</option>";
}
With the nested loop you will output three options for each item in $aMecaniciens, which I assume you don't want.

foreach($aMecaniciens as $key => $value){
echo '<option value="'.$value['idMecanicien'].'">'.$value['vchNomMecanicien'].' '.$value['vchPrenomMecanicien'].'</option>';
}

Related

i run this code but it donot type the array

$studentArray = array(
array("ahmed2",15,3.5),
array("ahmed1",15,2.4),
array("ahmed3",29,3.9),
array("ahmed4",22,3),
array("ahmed5",23,2.8)
);
foreach($studentArray as $key => $value ){
echo $key. '-'.$value.'<br>';
}
Since you have student detail array in $studentArray, you need an extra foreach to loop through inner array.
Try below code in your structure.
$studentArray = array(
array("ahmed2",15,3.5),
array("ahmed1",15,2.4),
array("ahmed3",29,3.9),
array("ahmed4",22,3),
array("ahmed5",23,2.8)
);
foreach($studentArray as $studentDetail ){
foreach($studentDetail as $key => $value ){
echo $key. '-'.$value.'<br>';
}
}
For more better understanding, have a try with below code.
$studentArray = array(
array("val1"=>"ahmed2","val2"=>15,"val3"=>3.5),
array("val1"=>"ahmed1","val2"=>15,"val3"=>2.4),
array("val1"=>"ahmed3","val2"=>29,"val3"=>3.9),
array("val1"=>"ahmed4","val2"=>22,"val3"=>3),
array("val1"=>"ahmed5","val2"=>23,"val3"=>2.8)
);
foreach($studentArray as $student){
foreach($student as $key => $value) {
echo $key. '-'.$value.'<br>';
}
echo '<br>';
}

PHP read value form multidimensional array

its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO

Set select box value from array

I have this UI:
Also, I have this PHP Back-end code:
<select name="as<?php echo $product['product_id']; ?>[]" style="width:250px;">
<?php
foreach ($product['uniSku'] as $key => $value) {
echo '<option value="'.$key.'">'.$value.'</option>';
}
?>
</select>
The question is, how can i put the data SKU from array into the select box?
You need to iterate twice in your multidimesional array
foreach ($product['uniSku'] as $data) {
foreach($data as $key => $val) {
echo '<option value="'.$key.'">'.$val.'</option>';
}
}
Or if you need to use keys from your parent array you can store it in a variable at first iteration
foreach ($product['uniSku'] as $kk => $data) {
foreach($data as $key => $val) {
echo '<option value="'.$kk.'">'.$val.'</option>';
}
}
Try this
Replace values with $value['sku']
foreach ($product['uniSku'] as $key => $value) {
echo '<option value="'.$key.'">'.$value['sku'].'</option>';
}
Here the solution
foreach ($product['uniSku'] as $key => $value) {
echo '<option value="'.$key.'">'.$value['sku'].'</option>';
}

How to get the $key from a multidimentional array using php?

Below is a simple array that I created:
$colors = array(
"parent1" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
),
"parent2" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
)
);
What I need to get is the key of my level 1 arrays which are string "parent1" and "parent2".
Currently I'm using foreach with while loop to get the key
foreach ($colors as $valuep) {
while (list($key, $value) = each($colors)) {
echo "$key<br />";
}
}
but I'm only able to get the "parent2" string from using the above method and not "parent1".
You're so close.
foreah($colors as $key => $val)
{
echo $key . "<br/>";
}
Use the key like so:
foreach ($colors as $key => $value) {
echo $key.'<br>';
}
To print out the keys:
foreach ($colors as $key => $value) {
echo $key . '<br />';
}
You can also get all of the keys from an array by using the array_keys() method, for example:
$keys = array_keys($colors);
foreach ($keys as $key) {
echo $key . '<br />';
}

PHP Merge Similar Objects In A Multidimensional Array

I have a multidimensional array in PHP, something that looks like:
array(array(Category => Video,
Value => 10.99),
array(Category => Video,
Value => 12.99),
array(Category => Music,
Value => 9.99)
)
and what I would like to do is combine similar categories and output everything into a table, so the output would end up being:
<tr><td>Video</td><td>23.98</td></tr>
<tr><td>Music</td><td>9.99</td></tr>
Any suggestions on how to do this?
EDIT:
I can have these in two different arrays if that would be easier.
A simple loop will do:
$array = [your array];
$result = array();
foreach ($array as $a) {
if (!isset($result[$a['Category']])) {
$result[$a['Category']] = $a['Value'];
} else {
$result[$a['Category']] += $a['Value'];
}
}
foreach ($result as $k => $v) {
echo '<tr><td>' . htmlspecialchars($k) . '</td><td>' . $v . '</td></tr>';
}
$result = array();
foreach ($array as $value) {
if (isset($result[$value['Category']])) {
$result[$value['Category']] += $value['Value'];
} else {
$result[$value['Category']] = $value['Value'];
}
}
foreach ($result as $category => $value) {
print "<tr><td>$category</td><td>$value</td></tr>";
}

Categories