Here is my array structure:
array (
'ALCAR STAHLRAD' =>
array (
'diametru' =>
array (
0 => 15,
6 => 16,
9 => 14,
14 => 13,
20 => 17,
468 => 20,
),
...........
I was doing that code:
$diametru = '';
foreach ($items as $key => $val){
$diametru .= "<option>$key[array]['diametru']=>$val</option>";
}
print_r($diametru);
I was expecting to add into my "option" the "diametru" value
<option>15</option>
<option>16</option>
<option>14</option>
..............
but the output is:
A['diametru']=>ArrayE['diametru']=>ArrayD['diametru']=>ArrayD['diametru']... Thank you in advance for any idea :)
-
1you have to use a nested foreach to get desired resultlaw_81– law_812018年10月24日 12:45:14 +00:00Commented Oct 24, 2018 at 12:45
-
It working just one foreach, I just need key of key value, then again key of key value, but I don't know how to extract that valuesGafwebmaster– Gafwebmaster2018年10月24日 12:49:21 +00:00Commented Oct 24, 2018 at 12:49
-
I was trying also: $diametru .= "<option>[$key][$key]=>$val</option>"; but still not extracting the value I need.Gafwebmaster– Gafwebmaster2018年10月24日 12:52:33 +00:00Commented Oct 24, 2018 at 12:52
2 Answers 2
Edit : If you want to add array_unique and SORT_NUMERIC then see my edited code.
You can do it through array_column and array_walk_recursive if you don't want to use foreach.
$arr = array ( 'ALCAR STAHLRAD' => array ( 'diametru' => array ( 0 => 15, 6 => 16, 7 => 16, 9 => 14, 14 => 13, 20 => 17, 468 => 20, ), ));
$option_arr = array_column($arr,'diametru');
function generate_option($item, $key)
{
echo "<option>" . $key . " : ". $item . "</option>";
}
$options = array_unique($option_arr[0]); // You can add array_unique and SORT_NUMERIC here
asort($options); // If you want sort by key then you need to use asort. Because array_unique remove duplicate from array but doesn't sort actually.
echo "<select>";
array_walk_recursive($options, 'generate_option');
echo "</select>";
answered Oct 24, 2018 at 12:55
Hardik Solanki
3,2151 gold badge21 silver badges30 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Gafwebmaster
Do you have any ideea on your sample where to add "array_unique" and "SORT_NUMERIC"
Gafwebmaster
Thank you again! The "array_unique" works fine but "SORT_NUMERIC" not :(
Gafwebmaster
I am using just "<option>" . $item . "</option>", maybe you sorted the $key values
Hardik Solanki
@Gafwebmaster Do you want sort by key or sort by value?
Gafwebmaster
I would like to sort by value
Get your 1st element first:
$alcar = $items[0]; // ALCAR STAHLRAD
// or
$alcar = $items['ALCAR STAHLRAD'];
Then get diameters array:
$diameters = $alcar['diametru'];
And only then you can do your loop:
diametru = '';
foreach ($diameters as $key => $val){
$diametru .= '<option>'.$key.'=>'.$val.'</option>';
}
print_r($diametru);
answered Oct 24, 2018 at 12:54
Alex
17.3k1 gold badge31 silver badges52 bronze badges
1 Comment
Gafwebmaster
The elements name comes dynamic, I don't know the name; it could be also one element or more, I can't go one by one to take and iterate for each.
lang-php