0

i have dynamic number of arrays and i need to make them a multidimensional array

Array ( [id] => 182 [name] => item1 ) 
Array ( [id] => 185 [name] => item2 ) 
Array ( [id] => 186 [name] => item3 ) 

to this

$all = Array ( Array ( [0] => Array ( [id] => 182 [name] => item1 ) , 
               Array ( [1] => Array ( [id] => 185 [name] => item2 ) ,
               Array ( [2] => Array ( [id] => 186 [name] => item3 ) 
             )

i tries this code but it's not working

$all_ids = Array ( [0] => 182 [1] => 185 [2] => 186 );
foreach($all_id as $id){
    // use code to return row
    $row= 'select id , name from table where id ='.$id; // something like this which return row
    $all= $all+ $row ;
}

any help please , thanks in advance

Ouda
  • 13
  • 4

2 Answers2

1

While I don't exactly understand what is to be achieved, you could declare $all as an array outside of the loop

$all = array();

and inside the loop, instead of

$all= $all+ $row ;

do

$all[] = $row;
eeetee
  • 502
  • 4
  • 14
0

It may be easier to change the SQL query to bring back all the records you want:

$all_ids = Array(182, 185, 186);
$sql = 'select id , name from table where id IN ('.implode(',',$all_ids).')';

Then call the database and fetch results. If you're using myql, then there is a mysqli call to fetch all results as an associative array: http://php.net/manual/en/mysqli-result.fetch-all.php

If you need to work out how to run queries and make a database connection then you should look around e.g.: mysqli connection and query

Community
  • 1
  • 1
Guillermo Phillips
  • 2,176
  • 1
  • 23
  • 40