0

I have an array codes like that.

Array (
[0] => Array 
    (
        [name] => pen
        [id] => 12
    )    
[1] => Array
    (
        [name] => pencil
        [id] => 34
    )       
[2] => Array
    (
        [name] => eraser
        [id] => 26
    )       
    .
    .
    .
    .       
[50] => Array
    (
        [name] => sharpener
        [id] => 53
    )    
)

I use print_r($res[0]["id"]) for list one id but I want to list all id. How can we do that? Thanks.

Joe J.
  • 19
  • 1
  • 6
  • use `foreach` to loop through – muya.dev Oct 16 '16 at 12:19
  • $a=count($res); for($i=0;$i<$a;$i++){echo $res[$i]['id'];} – Mo Shal Oct 16 '16 at 12:28
  • Thank you @mohade your code works fine but it's not work if I can use it on database. My code $sql = "select * from table where id like '%" . $res[$i]["id"] . "%'"; $result = mysql_query($sql); if ( mysql_num_rows($result) > 0 ) { $row = mysql_fetch_assoc( $result ); //print_r( $row); echo $row["name"]; } – Joe J. Oct 16 '16 at 13:33
  • you can use my code make an empty variable for example $r=""; and replace echo with if($i==0){$r=$res[$i][0].",";} else {$r.=",".$res[$i][0];} – Mo Shal Oct 16 '16 at 22:11
  • then your sql will be select * from table where id IN($r) – Mo Shal Oct 16 '16 at 22:12

0 Answers0