Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php for where string in array

Posted on 2016-08-04
9
Medium Priority
?
43 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 53

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 53

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

Industry Leaders: 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!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 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