Solved

How to retrieve the specified value from this array

Posted on 2011-03-01
9
206 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

695 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