Solved

How to retrieve the specified value from this array

Posted on 2011-03-01
9
202 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 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hasing a url 16 28
I wonder how people fake their ip address? 3 40
Eloquent ORM manual paginator defaults to simple 2 25
How do I fix this UPDATE error? 7 24
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

803 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