<?php
$list = array("one", "two", "three"=>"four", array("five", "six"));
echo $list[3][1];
?>
i want to output "five", but i'm facing problem with the above code..What is wrong?
How to use associative and multi-dimensional array together?
Moderator: General Moderators
-
d.stubborn.kanav
- Forum Newbie
- Posts: 1
- Joined: Fri Oct 18, 2013 3:56 am
Re: How to use associative and multi-dimensional array toget
wouldn't it be:
??
Code: Select all
echo $list[3][0];-
priyankagound
- Forum Commoner
- Posts: 27
- Joined: Thu Sep 19, 2013 2:53 am
Re: How to use associative and multi-dimensional array toget
You'll have to use PDOStatement::fetch() and populate the array by yourself, try out with the below example code:
$result = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// note that $row['user_id'] will currently a string
// if you need integers as indexes then use intval($row['user_id']);
$result[$row['user_id']] = $row;
}
// dump all records
var_dump($result);
// dump username of user with uid = 4
var_dump($result['4']['username']);
Hope this helps.
$result = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// note that $row['user_id'] will currently a string
// if you need integers as indexes then use intval($row['user_id']);
$result[$row['user_id']] = $row;
}
// dump all records
var_dump($result);
// dump username of user with uid = 4
var_dump($result['4']['username']);
Hope this helps.
Re: How to use associative and multi-dimensional array toget
No. There's no mention of databases anywhere in the OP.priyankagound wrote:You'll have to use PDOStatement::fetch()