First Steps in PHP - read and manipulate Arrays

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
tpm
Forum Newbie
Posts: 2
Joined: Tue Apr 20, 2004 4:37 am

First Steps in PHP - read and manipulate Arrays

Post by tpm »

Hi folks,

im just beginning to do some work in php. Please can anybody show me how i have to code this Javascript snippet in php:


var seitenname="name8";
var active_array_line="0";
var active_array_line_level="0";

var menu=new Array(
new Array('1','1','name1','url'),
new Array('2','0','name2','url'),
new Array('2','0','name3','url'),
new Array('2','0','name4','url'),
new Array('3','0','name5','url'),
new Array('3','0','name6','url'),
new Array('3','0','name7','url'),
new Array('2','0','name8','url'),
new Array('2','0','name9','url'),
new Array('3','0','name10','url')
);

for(i=0;i<menu.length;i++){
if(menu[2]==seitenname){
active_array_line=i;
active_array_line_level=menu[0]
}
}

THANKS
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Javascript array unclear

Post by Lord Sauron »

Mmm, creating the array with php would not be the problem, but I'm not vary good with Javascript.

The array you are creating, is that a 4-dimensional array? If so, what does it mean?
I really don't understand the following if-sentence:
if(menu[2]==seitenname){

This would mean to me, that you are trying to search for data in a 3-dimensional array, instead of a 4-dimensional array. And that you are trying to find data, where the 2nd position of the array is equal to 2. Which is nowhere. In other words, it doesn't make sence to me.

If you can explain you're Javascript to me, I will explain you how to create the array in php.

Kind regards,

Antonie
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

I think its two dimensional and i think it will be pretty much the same to create it - just drop the "new"s like $menu = array(array(1, 2...))

more info here

http://us2.php.net/manual/en/ref.array.php
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

2dimensional

Post by Lord Sauron »

Hi,

I'm not planning to read a Javascript tutorial (certainly not when the link is not working), but when you are trying to create a 2nd dimensional array, then why are there 4 variables?
new Array('1','1','name1','url')

Anyway, creating a php array is even easier

$menu[1][1]["name1"] = "url";
$menu[2][0]["name2"] = "url";
$menu[2][0]["name3"] = "url";

Although this would be a 3-dimension array, and I think that such a contruction would only make sense when the url is different every time.

Anyway, good luck with your 'problem'.

Kind regards,

Antonie
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

ok,

a) that was for tpm who asked the question
b)its not a javascript thing at all its from php.net
c)thats a 2 dimensiolan array with the second dimension full of 4 - long arrays
tpm
Forum Newbie
Posts: 2
Joined: Tue Apr 20, 2004 4:37 am

Post by tpm »

Thanks for your help,

the Array contains information for a dynamic menu. Each line of the menu-Array is a new Array with the information for one link containing the level (1,2,3...), the display status, the name of the link and the target url. - So every Line is definitely different...


So, if i understand it right, i have the array in PHP like this?

$menu = array(
array('level' => 1, 'display' => 1, 'lname' => name1, 'url' => someURL),
array('level' => 2, 'display' => 0, 'lname' => name2, 'url' => someURL),
array('level' => 3, 'display' => 0, 'lname' => name3, 'url' => someURL),
array('level' => 2, 'display' => 0, 'lname' => name4, 'url' => someURL),
array('level' => 1, 'display' => 1, 'lname' => name5, 'url' => someURL)
);


But how do i address the array to get the information?

For example, i want to search the array for the information "name3" and get the position in the array for further operations.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

look pu foreach and while at php.net
User avatar
Lord Sauron
Forum Commoner
Posts: 85
Joined: Tue Apr 20, 2004 5:53 am
Location: Tilburg, NL

Or something like this

Post by Lord Sauron »

Depends on what information you want. If you want the url and you constructed the array as I suggested, you could use:

ECHO $menu[1][1]["name1"];
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

For the array as you described above try

Code: Select all

<?php
 foreach($menu as $item){
    echo $item['lname']." ".$item['url'];
  }
?>
Post Reply