Solved

How to retrieve the specified value from this array

Posted on 2011-03-01
9
204 Views
Last Modified: 2012-05-11
I described my question on below codes.

Simplified, here is a string $var['navy'] = 'arestama';
After codes processing, I'm able to retrieve 'arestama' after matching it with $selected_value but I cannot retrieve 'navy'

Can anybody help?  Thanks all...
$var['blue'] = 'johndoes'; // #1
$var['pink'] = 'demolimo'; // #2
$var['navy'] = 'arestama'; // #3
$var['bait'] = 'kikotito'; // #4

foreach($var as $var_arr=>$val_arr) { 
		$var_in_arr[] = $var_arr; // blue, pink, navy, bait
		$val_in_arr[] = $val_arr; // johndoes, demolimo, arestama, kikotito
}

// A selected value might be one of the $var above
// In this case let's use #3
$selected_value = 'arestama';

if ( ( in_array($selected_value, $val_in_arr) ) ) {
	$value = $selected_value;
}

echo ( isset($value)?$value:'Unknown' );
// So far the output here is correct & match which is 'arestama'

// Question:
// How to output 'navy' here ???

Open in new window

0
Comment
Question by:trrsrr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35007100
YOu have two arrays val_in_arr and var_in_arr. This code

if ( ( in_array($selected_value, $val_in_arr) ) ) {
      $value = $selected_value;
}

only searches val_in_arr. "Navy" is in the other array
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35007119
<?php

$var['blue'] = 'johndoes'; // #1
$var['pink'] = 'demolimo'; // #2
$var['navy'] = 'arestama'; // #3
$var['bait'] = 'kikotito'; // #4

foreach($var as $var_arr=>$val_arr) {
          $var_in_arr[] = $var_arr; // blue, pink, navy, bait
          $val_in_arr[] = $val_arr; // johndoes, demolimo, arestama, kikotito
}


// A selected value might be one of the $var above
// In this case let's use #3
$selected_value = 'arestama';

if ( ( in_array($selected_value, $val_in_arr) ) ) {
     $value = $selected_value;
}


echo ( isset($value)?$value:'Unknown' );
// So far the output here is correct & match which is 'arestama'

// Question:
// How to output 'navy' here ???
echo "<br/>";
$selected_value = 'navy';

if ( ( in_array($selected_value, $var_in_arr) ) ) {
     $value = $selected_value;
}

echo ( isset($value)?$value:'Unknown' );

Open in new window


Produces

arestama
navy
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 300 total points
ID: 35007156
<?php
$var['blue'] = 'johndoes'; // #1
$var['pink'] = 'demolimo'; // #2
$var['navy'] = 'arestama'; // #3
$var['bait'] = 'kikotito'; // #4

foreach($var as $var_arr=>$val_arr) { 
		$var_in_arr[] = $var_arr; // blue, pink, navy, bait
		$val_in_arr[] = $val_arr; // johndoes, demolimo, arestama, kikotito
}

// A selected value might be one of the $var above
// In this case let's use #3
$selected_value = 'arestama';

if ( ( in_array($selected_value, $val_in_arr) ) ) {
	$value = $selected_value;
}

echo ( isset($value)?$value:'Unknown' );


reset($var);
while ($test = current($var)) {
    if ($test == $value) {
        echo key($var).'<br />';
    }
    next($var);
}

// So far the output here is correct & match which is 'arestama'

// Question:
// How to output 'navy' here ???
?>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:trrsrr
ID: 35007187
Note that: I can only have 1 $selected_value ..
actually it is taken from GET variable and I have shorten my codes which I think will give me the same result .. and for sure I cannot have $selected_value = 'navy';

Any idea ?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35007220
Did you try this above ?

reset($var);
while ($test = current($var)) {
    if ($test == $value) {
        echo key($var).'<br />';
    }
    next($var);
}

Open in new window

0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 200 total points
ID: 35007297
OK.....

<?php

$var['blue'] = 'johndoes'; // #1
$var['pink'] = 'demolimo'; // #2
$var['navy'] = 'arestama'; // #3
$var['bait'] = 'kikotito'; // #4

foreach($var as $var_arr=>$val_arr) {
          $var_in_arr[] = $var_arr; // blue, pink, navy, bait
          $val_in_arr[] = $val_arr; // johndoes, demolimo, arestama, kikotito
}


// A selected value might be one of the $var above
// In this case let's use #3
$selected_value = 'arestama';

if ( ( in_array($selected_value, $val_in_arr) ) ) {
     $value = $selected_value;
}


if ( isset($value) ) {
     echo $value . " ".   array_search( $value, $var );
}
else
     echo 'Unknown';

Open in new window

0
 
LVL 1

Author Comment

by:trrsrr
ID: 35007305
@Roads_Roads:
Yes ... my previous reply was to @bportlock (forgot to mentioned it).

And thanks it looks good for my purposes.
I also have seen this reset() key() and next() .. lol :-)
Got to learn some more and more I guess...

Thanks a lot.
0
 
LVL 1

Author Closing Comment

by:trrsrr
ID: 35007354
Okay ... @bportlock seeme to have short codes solution which gave me the same result.

Well, I think I'll split the points to you guys for teach me more ideas .. :)

Thanks a lot.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35007389
Glad you're sorted
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question