Solved

php for where string in array

Posted on 2016-08-04
9
39 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 51

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 8

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 8

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 8

Expert Comment

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hbo knew my windows software 4 62
display balance of stars as greyed out 8 23
Log in through ID 5 17
Add Logo to Bookmark and Tab Headings in Browser 3 12
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.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 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