Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php for where string in array

Posted on 2016-08-04
9
Medium Priority
?
44 Views
Last Modified: 2016-08-04
See my array and for repeat below...

$email_sigs = array(
				array("first_name"=>"Aaron", "last_name"=>"Clarke", "name_link"=>"aaron_clark"),
				array("first_name"=>"Alison", "last_name"=>"McNulty", "name_link"=>"alison_mcnulty"),
				array("first_name"=>"Amira", "last_name"=>"Bouchiba", "name_link"=>"amira_bouchiba")
			  );

$arrlength = count($email_sigs);

<?php for($num = 0; $num < $arrlength; $num++) { ?>

<p><?php echo $email_sigs[$num]["first_name"]; ?> <?php echo $email_sigs[$num]['last_name']; ?></p>

<?php } ?>

Open in new window


Can you please show me an example how I only show the array row where the url parameter ( $_GET['name'] ) equals the "name_link" field?
0
Comment
Question by:petewinter
  • 4
  • 3
  • 2
9 Comments
 
LVL 54

Assisted Solution

by:Ryan Chong
Ryan Chong earned 1000 total points
ID: 41741925
try:
<?php

$email_sigs = array(
				array("first_name"=>"Aaron", "last_name"=>"Clarke", "name_link"=>"aaron_clark"),
				array("first_name"=>"Alison", "last_name"=>"McNulty", "name_link"=>"alison_mcnulty"),
				array("first_name"=>"Amira", "last_name"=>"Bouchiba", "name_link"=>"amira_bouchiba")
			  );

$arrlength = count($email_sigs);

for($num = 0; $num < $arrlength; $num++) { 
  if ($_GET['name'] == $email_sigs[$num]["name_link"]) {					  
?>
<p><?php echo $email_sigs[$num]["first_name"]; ?> <?php echo $email_sigs[$num]['last_name']; ?></p>

<?php  }
	 }
?>

Open in new window

0
 
LVL 9

Accepted Solution

by:
Mukesh Yadav earned 1000 total points
ID: 41741934
Try this ;)

/* restructure array use name_link value as key; */
$email_sigs = array(
    "aaron_clark" => array(
        "first_name" => "Aaron",
        "last_name" => "Clarke"),
    "alison_mcnulty" => array(
        "first_name" => "Alison",
        "last_name" => "McNulty"),
    "amira_bouchiba" => array(
        "first_name" => "Amira",
        "last_name" => "Bouchiba"));

$name = $_GET['name'];
if(array_key_exists($name, $email_sigs)){
  echo '<p>' . $email_sigs[$name]["first_name"] . ' ' . $email_sigs[$name]['last_name'] . '</p>';
}

Open in new window

0
 

Author Comment

by:petewinter
ID: 41741954
Thanks to you both. You have suggested two different methods. Does it make any difference which one I use? Any load speed difference, etc?
1
Technology Partners: 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!

 

Author Comment

by:petewinter
ID: 41742162
Ryan Chong - In theory your suggested version is doing the loop for each row, then just hiding the result. Would that take longer to load if it's a large array?
0
 
LVL 9

Expert Comment

by:Mukesh Yadav
ID: 41742167
It totally depends on you if you are able to restructure the $email_sigs array then you should go with my solution. :)

If you have any concern please let me know.
0
 

Author Comment

by:petewinter
ID: 41742172
I would actually prefer to not restructure, but does it have any speed load difference?
1
 
LVL 9

Expert Comment

by:Mukesh Yadav
ID: 41742175
Yes it has.
0
 
LVL 54

Assisted Solution

by:Ryan Chong
Ryan Chong earned 1000 total points
ID: 41742198
>>Would that take longer to load if it's a large array?
yes it will, but if it's a large array, why you want to do a loop at the first place?

anyway, you can try something like this, which may perform better compare with the if condition.
<?php

$email_sigs = array(
				array("first_name"=>"Aaron", "last_name"=>"Clarke", "name_link"=>"aaron_clark"),
				array("first_name"=>"Alison", "last_name"=>"McNulty", "name_link"=>"alison_mcnulty"),
				array("first_name"=>"Amira", "last_name"=>"Bouchiba", "name_link"=>"amira_bouchiba")
			  );

$GLOBALS['name'] = $_GET['name'];

$rst = array_filter($email_sigs, function ($var) { return ($var["name_link"]== $GLOBALS['name']); } );

foreach($rst as $item) {
	?>
	<p><?php echo $item['first_name']; ?> <?php echo $item['last_name']; ?></p>
    <?php
}
	
?>

Open in new window

0
 

Author Closing Comment

by:petewinter
ID: 41742221
Many thanks for your examples and advice.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 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…
Suggested Courses

916 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