Solved

php for where string in array

Posted on 2016-08-04
9
36 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 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 250 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 6

Accepted Solution

by:
Mukesh Yadav earned 250 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 6

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 6

Expert Comment

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

Assisted Solution

by:Ryan Chong
Ryan Chong earned 250 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

758 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

20 Experts available now in Live!

Get 1:1 Help Now