954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Arrays

I have learned php this summer so i am pritty new and need some help.

what i am trying to do is that i have an item with an item# and a lot of other info from my db and would like to transfer that info in the array in a way so that when i select an item # it would display item# aswell as the data that was put in an arry. what command or how would i do that.

Thanks

ashneet
Junior Poster
147 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

Well, from your post, I'm unclear as to what exactly you want to do, but I hope this helps:
[php]
<?php
$var = array('1' => 'foo'
'2' => 'bar'
'heh' => 'Hello, World!');
echo $array['1'] . '-' . $array['2'] . '-' . $array['heh'];
// The following would echo 'foo-bar-Hello, World!';
?>
[/php]

Sp!ke
Light Poster
45 posts since Jul 2005
Reputation Points: 14
Solved Threads: 0
 

i think i can sove it but what i really wanted to know is that is it possible to have more then one result to an key like in your example if i told array to display 1 it would display foo As item name and item#.

ashneet
Junior Poster
147 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

Man, I love Sp!ke's posts. He posts beautiful PHP code with excellent comments. Message to all--don't forget to use the post rating system to thank people who help you.

Ashneet, I think what you want is simply an array of arrays. PHP does not support true multi-dimensional arrays, but an array of arrays will fit the bill.

Put this code into a new PHP file and try it:
[PHP]
<?php

$items = array(
1=>array('name'=>'apple','color'=>'red','price'=>0.50)
,2=>array('name'=>'orange','color'=>'orange','price'=>0.60)
,3=>array('name'=>'banana','color'=>'yellow','price'=>0.35)
);

echo "";
print_r($items);
echo "";

?>
[/PHP]

Will output:

Array
(
    [1] => Array
        (
            [name] => apple
            [color] => red
            [price] => 0.5
        )

    [2] => Array
        (
            [name] => orange
            [color] => orange
            [price] => 0.6
        )

    [3] => Array
        (
            [name] => banana
            [color] => yellow
            [price] => 0.35
        )

)

So you can access individual items like so:
[PHP]
echo $items[2][color];
[/PHP]

Of course you would build your array dynamically from your database query.

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

Thanks That make sense and it helped as that will do the job really well

ashneet
Junior Poster
147 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You