Creating a Select from a multidimensional array

multidimensional array php
php multidimensional array foreach
php create multidimensional array in loop
multidimensional array javascript
php multidimensional associative array
php multidimensional array with keys
get value from multidimensional array php
how to print multidimensional array in php using for loop

So I have an array from a database query, it's something like this:

Array (

[0] => Array
    (
        [0] => 1
        [1] => Name1
    )

[1] => Array
    (
        [0] => 3
        [1] => Name2
    )

[2] => Array
    (
        [0] => 4
        [1] => Name3
    )

I'm trying to create a Select from that (it has +50 arrays, not only 2) and I tried doing this:

<select name="client" onchange="this.form.submit()">
                <option value="" disabled selected>--client--</option>
                <?php
                    for ($i = 0; $i < count($idnameCli); $i++) { 
                        echo "<option value='$idnameCli[$i][1]'>$idnameCli[$i][0] $idnomCli[$i][1] </option>";
                    }
                ?>
</select>

It is not working because you've to use quotes or escape those. You can do it simply using implode(),

<select name="client" onchange="this.form.submit()">
                <option value="" disabled selected>--client--</option>
                <?php
                    for ($i = 0; $i < count($idnameCli); $i++) { 
                        echo "<option value='".$idnameCli[$i][1]."'>". implode('',$idnameCli[$i]) ."</option>";
                    }
                ?>
</select>

DEMO: https://3v4l.org/TATGZ

Creating a html Select menu from multidimensional array, Creating a html Select menu from multidimensional array. "Hi Guys,. I have a 2 dimensional array as the below: Array ( [0] => Array ( [lobID] => 1 [lobName]� Rectangular arrays are not built for this purpose. If you need that type of functionality, you should switch to a jagged array. It is pretty simple to write a function that will convert a rectangular array into a jagged one.

Hi and welcome on Stackoverflow,

I had lot of difficulty to read your question so i hope i've understood what you want.

<select>
<?php for($i = 0; $i<sizeof($prova); $i++){
    echo "<option value=\"".$arr[$i][0]."\">".$arr[$i][0]."-".$arr[$i][1]."</option> ";
} ?>
</select>

PHP Multidimensional Arrays, Well organized and easy to understand Web building tutorials with lots of For a two-dimensional array you need two indices to select an element; For a� Creating Multidimensional Arrays. You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array.

See manual : String Operators

When you write :

echo "<option value='$idnameCli[$i][1]'> ...

It's like you want to set a value for that option which is the string '$idnameCli[$i][1]' ; if you wanted the value to be "johnny" you would write "<option value='johnny'>

In PHP you concatenate strings and variables with "." like :

"<option value='" . $idnameCli[$i][1] . "'> ...

And also in your case two variables next to each other (here with a space between them) :

echo "<option value='" . $idnameCli[$i][1] . "'>" . $idnameCli[$i][0] . " " . $idnomCli[$i][1] . "</option>"; 

Multidimensional arrays in PHP, For a two dimensional array two indices to select an element. Two dimensional array: It is the simplest form of a multidimensional array. It can be created using� Inserting a Multi-dimensional Array during Runtime: This topic is forced n taking user-defined input into a multidimensional array during runtime. It is focused on the user first giving all the input to the program during runtime and after all entered input, the program will give output with respect to each input accordingly.

Multidimensional array - handling SQL results - PHP, Sure, well basically with the first query it creates a new entry in the array using the person_id as a key. When we loop through the second query, we use the� Multidimensional arrays are one of the complex data types supported by PowerShell. In simple let you imagine a multidimensional array like a table, with columns and rows, where each cell has its own index like [1,16). Each time we put a comma, we are like telling Powershell to start a new row in the multidimensional array

Documentation: 9.1: Arrays, The syntax for CREATE TABLE allows the exact size of arrays to be specified, for example: CREATE SELECT * FROM sal_emp; name | pay_by_quarter | schedule Multidimensional arrays must have matching extents for each dimension. It is a 2-dimensional array, that can hold a maximum of 12 elements, 2-dimensional Array. Remember, Java uses zero-based indexing, that is, indexing of arrays in Java starts with 0 and not 1. Let's take another example of the multidimensional array. This time we will be creating a 3-dimensional array. For example, String[][][] data = new String

Multidimensional Arrays - MATLAB & Simulink, Creating Multidimensional Arrays. You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-� Multidimensional Arrays. As the name indicates, multi dimensional arrays are arrays that contain more than one dimension, usually two or three but it can have up to 32 dimensions. A multi array works like a matrix with various levels, take in example a comparison between one, two, and three Dimensions.

Comments
  • This is exactly what I was looking for, didn't knew implode exist that makes it much cleaner! I'm kinda new to php and I still mess up with the quotes. Thank you!
  • @TraaZ glad it helps you somehow, Best of luck :)
  • I never used "\" in an echo call I'm not sure what it does but it does work!
  • In this case "\" considers the next character as part of the string instead of a delimiter. Else the " would end the string.
  • Thanks, very useful ^^