0

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 :)

3
  • 1
    you have to use a nested foreach to get desired result Commented 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 values Commented Oct 24, 2018 at 12:49
  • I was trying also: $diametru .= "<option>[$key][$key]=>$val</option>"; but still not extracting the value I need. Commented Oct 24, 2018 at 12:52

2 Answers 2

1

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>";
Sign up to request clarification or add additional context in comments.

5 Comments

Do you have any ideea on your sample where to add "array_unique" and "SORT_NUMERIC"
Thank you again! The "array_unique" works fine but "SORT_NUMERIC" not :(
I am using just "<option>" . $item . "</option>", maybe you sorted the $key values
@Gafwebmaster Do you want sort by key or sort by value?
I would like to sort by value
0

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);

1 Comment

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.

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.