How to use associative and multi-dimensional array together?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
d.stubborn.kanav
Forum Newbie
Posts: 1
Joined: Fri Oct 18, 2013 3:56 am

How to use associative and multi-dimensional array together?

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to use associative and multi-dimensional array toget

Post by Celauran »

Code: Select all

print_r($list);
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: How to use associative and multi-dimensional array toget

Post by Burrito »

wouldn't it be:

Code: Select all

echo $list[3][0];
??
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to use associative and multi-dimensional array toget

Post by Celauran »

Code: Select all

echo $list[2][0];
priyankagound
Forum Commoner
Posts: 27
Joined: Thu Sep 19, 2013 2:53 am

Re: How to use associative and multi-dimensional array toget

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to use associative and multi-dimensional array toget

Post by Celauran »

priyankagound wrote:You'll have to use PDOStatement::fetch()
No. There's no mention of databases anywhere in the OP.
Post Reply