Current location: Hot Scripts Forums » Programming Languages » PHP » array index question


array index question

Reply
  #1 (permalink)  
Old 07-01-09, 09:12 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
array index question

Hi there,

I got this array issue:

this array I use on my site:

PHP Code:

$nbr = Array ( [solutionactief1] => yes, [solutionpositie1] =>, [solutionactief2] => yes, [solutionpositie2] => [eventpositie3] => ); 

I search this array for a key with the value "yes"
PHP Code:

$tel_yes 0;
foreach (
$nbr as $value) {
    if(
$value =="yes"){    
        echo 
"- ".$value." | ".$tel_yes."<br>";
    }    
    
$tel_yes++;
}                            
unset(
$value); 
Now I'm looking to get the name of the index. F.E. if "solutionactief1" is "yes" I need the "solutionposition1" as well.
So I thought to substract the "1" out of the index name and combine this with "solutionposition" to get "solutionposition1".

But I can't catch the index name.. Who can help me out here?.

_j
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-01-09, 09:29 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
PHP: foreach - Manual

Very first example...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-01-09, 09:57 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If I understand you correctly, you want to get the name of the key and the value from that key that is directly after any key that has a value of "yes".

If this is correct, then this will do it.
PHP Code:

<?php
$nbr 
= array ( "solutionactief1" => "yes""solutionpositie1" => "Some value #1""solutionactief2" => "yes""solutionpositie2" => "Some value #2""eventpositie3" => "");
$keys array_keys($nbr);
foreach(
$nbr as $key => $value)
{
 if(
$value == "yes")
 {
  echo 
"- ".$value." | ".$key."<br />";
  for(
$i=0;$i<count($keys);$i++)
  {
   if(
$key == $keys[$i])
   {
    if(isset(
$keys[$i+1])){echo "- ".$nbr[$keys[$i+1]]." | ".$keys[$i+1]."<br />";}
    }
   }
  }
 }
unset(
$keys);
unset(
$key);
unset(
$value);
?>
Or maybe on one line like this:
PHP Code:

<?php
$nbr 
= array ( "solutionactief1" => "yes""solutionpositie1" => "Some value #1""solutionactief2" => "yes""solutionpositie2" => "Some value #2""eventpositie3" => "");
$keys array_keys($nbr);
foreach(
$nbr as $key => $value)
{
 if(
$value == "yes")
 {
  echo 
"- ".$value." | ".$key;
  for(
$i=0;$i<count($keys);$i++)
  {
   if(
$key == $keys[$i])
   {
    if(isset(
$keys[$i+1])){echo " --- ".$nbr[$keys[$i+1]]." | ".$keys[$i+1]."<br />";}
    else{echo 
"<br />";}
    }
   }
  }
 }
unset(
$keys);
unset(
$key);
unset(
$value);
?>
__________________
Jerry Broughton

Last edited by job0107; 07-01-09 at 10:25 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove value from array Nikas PHP 1 05-20-09 04:20 AM
Multi-Dimensional Array Help Nikas PHP 17 05-04-09 03:10 AM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 04:54 PM
getting combinations of array index or elements karthikeyan PHP 1 03-27-06 12:11 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 09:14 PM


All times are GMT -5. The time now is 01:38 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.