First Steps in PHP - read and manipulate Arrays
Moderator: General Moderators
First Steps in PHP - read and manipulate Arrays
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
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
- Lord Sauron
- Forum Commoner
- Posts: 85
- Joined: Tue Apr 20, 2004 5:53 am
- Location: Tilburg, NL
Javascript array unclear
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
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
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
more info here
http://us2.php.net/manual/en/ref.array.php
- Lord Sauron
- Forum Commoner
- Posts: 85
- Joined: Tue Apr 20, 2004 5:53 am
- Location: Tilburg, NL
2dimensional
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
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
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.
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
- Lord Sauron
- Forum Commoner
- Posts: 85
- Joined: Tue Apr 20, 2004 5:53 am
- Location: Tilburg, NL
Or something like this
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"];
ECHO $menu[1][1]["name1"];
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
For the array as you described above try
Code: Select all
<?php
foreach($menu as $item){
echo $item['lname']." ".$item['url'];
}
?>