Solved

php for where string in array

Posted on 2016-08-04
9
41 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 52

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 9

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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 52

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
curl parse data from site 20 66
WordPress  Failed to Import Media 8 65
Change background images after 5 seconds. 12 47
Developers / Staff Setup 10 43
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…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 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