Solved

How to retrieve the specified value from this array

Posted on 2011-03-01
9
200 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now