Aneeq Tariq
Aneeq Tariq

Reputation: 436

creating multidimensional array from single array

i have a html form in which i use array like this (name="courts[]"). when it send data to php file i use foreeach loop to create multidimensional array for inserting records in mysql. In php file i write foreeach loop to iterate like this

    $data = array();
    $i = 0;
    foreach ($court_name as $result)
    {
        $data[] = array(
            'court_name' => $result[0]
        );
        $i++;
    }

it display result this

 Array
 (
      [0] => Array
       (
            [court_name] => P
       )

      [1] => Array
      (
           [court_name] => S
      )

 )

instead of this

 Array
 (
      [0] => Array
       (
            [court_name] => Punjab
       )

      [1] => Array
      (
           [court_name] => Sindh
      )

 )

Upvotes: 0

Views: 449

Answers (2)

mychalvlcek
mychalvlcek

Reputation: 4046

foreach loop gives you one element of array ($result) now you access to the first character of value via $result[0], change it to $result

foreach ($court_name as $result) {
  $data[] = array( 'court_name' => $result );
}

Upvotes: 0

aykut
aykut

Reputation: 3094

(referring the outputs) in your loop, $result contains court name. So if you use $result[0], you get first character of string.

Try this:

foreach ($court_name as $result)
{
    $data[] = array(
        'court_name' => $result
    );
    $i++;
}

Upvotes: 2

Related Questions