Page 1 of 1
How to use associative and multi-dimensional array together?
Posted: Fri Oct 18, 2013 4:09 am
by d.stubborn.kanav
<?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?
Re: How to use associative and multi-dimensional array toget
Posted: Fri Oct 18, 2013 6:14 am
by Celauran
Re: How to use associative and multi-dimensional array toget
Posted: Fri Oct 18, 2013 10:06 am
by Burrito
Re: How to use associative and multi-dimensional array toget
Posted: Fri Oct 18, 2013 10:26 am
by Celauran
Re: How to use associative and multi-dimensional array toget
Posted: Sat Oct 19, 2013 5:41 am
by priyankagound
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.
Re: How to use associative and multi-dimensional array toget
Posted: Sat Oct 19, 2013 7:12 am
by Celauran
priyankagound wrote:You'll have to use PDOStatement::fetch()
No. There's no mention of databases anywhere in the OP.