[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to retrieve the specified value from this array

Posted on 2011-03-01
9
Medium Priority
?
209 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
  • 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 1200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 800 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

873 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