Jump to content

Recommended Posts

I have a session array that is added to by a form. Here it is when the form is submitted twice. It can grow larger and larger in a single session but the keys are always the same. What I need to do is check all the arrays to see if the key called Form matches a variable and if it matches then I will use that values in that particular array.

 

array
0 =>
array
 'Age' => string '20' (length=2)
 'City' => string 'New York' (length=
 'Your_name' => string 'Jim' (length=3)
 'Friend1' => string 'Joseph' (length=6)
 'Friend2' => string 'Waldo' (length=5)
 'Form' => string 'help' (length=4)
1 =>
array
 'Age' => string '25' (length=2)
 'City' => string 'Sun Valley' (length=10)
 'Your_name' => string 'Susan' (length=5)
 'Friend1' => string 'Rick' (length=4)
 'Friend2' => string 'Joe' (length=3)
 'Form' => string 'welcome' (length=4)

 

I know that I can do something like this to check the first one.

 

$key = 'help';
$array = $_SESSION['array'];
if($array[0]['Form'] == $key){
echo "We have a match";
}

 

But how can I automatically cycle through [0], [1], and so on until I find a match?

 

Thank you.

Edited by tqla
Link to comment
https://forums.phpfreaks.com/topic/273934-array-help/
Share on other sites

If the "Form" is a unique string then why not use it as the array key instead of the numbers?

array
'help' =>
array
         'Age' => string '20' (length=2)
         'City' => string 'New York' (length=
         'Your_name' => string 'Jim' (length=3)
         'Friend1' => string 'Joseph' (length=6)
         'Friend2' => string 'Waldo' (length=5)
         'Form' => string 'help' (length=4)
'welcome' =>
array
         'Age' => string '25' (length=2)
         'City' => string 'Sun Valley' (length=10)
         'Your_name' => string 'Susan' (length=5)
         'Friend1' => string 'Rick' (length=4)
         'Friend2' => string 'Joe' (length=3)
         'Form' => string 'welcome' (length=4)

$key = 'help';
$array = $_SESSION['array'];
if(isset($array[$key])){
echo "We have a match";
}

Edited by requinix
Link to comment
https://forums.phpfreaks.com/topic/273934-array-help/#findComment-1409639
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.