How To get Index from Array and Select Spicific array

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
shafiq2626
Forum Commoner
Posts: 88
Joined: Wed Mar 04, 2009 1:54 am
Location: Lahore
Contact:

How To get Index from Array and Select Spicific array

Post by shafiq2626 »

Hello!
i stored values in array like id,name,password and email. like

Code: Select all

<?php $databse=array(
    "ID"=>"1", array("name"=>"shafique", "password"=>"123", "email"=>"email@email.com"),
    "ID"=>"2", array("name"=>"sha", "password"=>"12", "email"=>"sha@email.com"),
    "ID"=>"3", array("name"=>"aaa", "password"=>"13", "aaa"=>"aaa@email.com")
); ?>
 
And after i want to log in from a form input to against a specific array value like name and password And also these arrays values should be Add ,edit,delete, these values from take the values by input .
please help me. :banghead:
thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How To get Index from Array and Select Spicific array

Post by Christopher »

What you are actually doing is this:

Code: Select all

<?php $databse=array(
    "ID"=>"1", 
    array("name"=>"shafique", "password"=>"123", "email"=>"email@email.com"),
    "ID"=>"2", 
    array("name"=>"sha", "password"=>"12", "email"=>"sha@email.com"),
    "ID"=>"3", 
    array("name"=>"aaa", "password"=>"13", "aaa"=>"aaa@email.com")
); ?>
The three arrays with be assigned indexes 0, 1, 2. And I assume that 'ID' will be 3, but that should give an error. You should use var_dump() or print_r to show $databse so you can see what you are creating.
(#10850)
shafiq2626
Forum Commoner
Posts: 88
Joined: Wed Mar 04, 2009 1:54 am
Location: Lahore
Contact:

Re: How change Array and with Spicific array value

Post by shafiq2626 »

Helo! I got Index now i want to change an array attributes to get the value from input.
like

Code: Select all

array("name"=>"shafique")
replace with 
array("name"=>"ssssss")
how this can possible .
please help me.
thanks
shafiq2626
Forum Commoner
Posts: 88
Joined: Wed Mar 04, 2009 1:54 am
Location: Lahore
Contact:

array output problem

Post by shafiq2626 »

hello!
i Make This Array

Code: Select all

$databse=array(
    "ID"=>"1",
     array("name"=>"shafique", "password"=>"123", "email"=>"email@email.com"),
    "ID"=>"2",
    array("name"=>"sha", "password"=>"12", "email"=>"sha@email.com"),
    "ID"=>"3",
     array("name"=>"aaa", "password"=>"13", "email"=>"aaa@email.com"));
for output

<?php

Code: Select all

foreach($databse as $key=>$value)
{
?>
<tr><td><?php echo $value['name'];?></td><td><?php echo $value['password'];?></td><td><?php echo $value['email'];?></td></tr>
<?php }?>
 

the result show
3 3 3
shafique 123 email@email.com
sha 12 sha@email.com
aaa 13 aaa@email.com



why the top row the values 3 3 3
the three rows are true but top row why display.

display please help

thanks
Last edited by Benjamin on Wed May 20, 2009 7:56 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How To get Index from Array and Select Spicific array

Post by Christopher »

Please do not double post.

Did you use var_dump() or print_r() like I suggested so you can see what your array actually looks like?
(#10850)
shafiq2626
Forum Commoner
Posts: 88
Joined: Wed Mar 04, 2009 1:54 am
Location: Lahore
Contact:

Re: How To get Index from Array and Select Spicific array

Post by shafiq2626 »

arborint wrote:Please do not double post.

Did you use var_dump() or print_r() like I suggested so you can see what your array actually looks like?
when i use var_dump($database);

then result show

array(4) { ["ID"]=> string(1) "3" [0]=> array(3) { ["name"]=> string(8) "shafique" ["password"]=> string(3) "123" ["email"]=> string(15) "email@email.com" } [1]=> array(3) { ["name"]=> string(3) "sha" ["password"]=> string(2) "12" ["email"]=> string(13) "sha@email.com" } [2]=> array(3) { ["name"]=> string(3) "aaa" ["password"]=> string(2) "13" ["email"]=> string(13) "aaa@email.com" } }
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How To get Index from Array and Select Spicific array

Post by Christopher »

A good PHP development trick is to do this:

Code: Select all

echo '<pre>' . print_r($var, 1) . '</pre>';
If you look at the array that way it will look something like this (but more expanded):

Code: Select all

array(4) {
 ["ID"]=>  string(1) "3"
 [0]=>  array(3) { ["name"]=>  string(8) "shafique" ["password"]=>  string(3) "123" ["email"]=>  string(15) "email@email.com" }
 [1]=>  array(3) { ["name"]=>  string(3) "sha" ["password"]=>  string(2) "12" ["email"]=>  string(13) "sha@email.com" }
 [2]=>  array(3) { ["name"]=>  string(3) "aaa" ["password"]=>  string(2) "13" ["email"]=>  string(13) "aaa@email.com" }
}
So it looks like your ['ID'] key is not doing what you thought is was doing. But the other elements look like what you want.
(#10850)
Post Reply