dweb Posted November 4, 2012 Share Posted November 4, 2012 (edited) Hi every body I have a multidimensional array that looks like $staff = array( array("David", 31 , 'HR'), array("Julie", 29 , 'Direct Sales'), array("John", 32 , 'Admin'), array("Shawn", 30 , 'Direct Support'), array("Vicki", 41 , 'HR') ); As you can see, it stores [persons name],[extension number],[department], in total I have about 30 rows on that array I then have several PHP pages, an example looks like; $page_text = 'If you need to contact Direct Sales or Admin, please call us'; echo $page_text; What I need to do is look at that var $page_text and check if any of the rows in the multidimensional array match any keywords, and then display them. So for example, with $page_text = 'If you need to contact HR or Admin, please call us"; It would output the following text David on ext 31 Vicki on ext 41 John on ext 32 If possible, I also wanted to make the extension number (ie: 41) a direct http link to their blog, which is /blog/staff/{EXT]/read/ so it might look like David on <a href="/blog/staff/31/read/">ext 31</a> Vicki on <a href="/blog/staff/41/read/">ext 41</a> John on <a href="/blog/staff/32/read/">ext 32</a> Can anyone help Thanks Edited November 4, 2012 by dweb Quote Link to comment Share on other sites More sharing options...
Christian F. Posted November 4, 2012 Share Posted November 4, 2012 The quickest way would probably be to edit the array slightly, so that is uses the following syntax: $staff = array ( 'HR' => array("David", 31), 'Direct Sales' => array("Julie", 29), 'Admin' => array("John", 32), 'Direct Support' => array("Shawn", 30), 'HR' => array("Vicki", 41) ); Then just use a foreach () loop to fetch the keys, and check if they're in the string. If so, add the values of the current key into a temp variable for later output. Quote Link to comment Share on other sites More sharing options...
dweb Posted November 4, 2012 Author Share Posted November 4, 2012 The quickest way would probably be to edit the array slightly, so that is uses the following syntax: $staff = array ( 'HR' => array("David", 31), 'Direct Sales' => array("Julie", 29), 'Admin' => array("John", 32), 'Direct Support' => array("Shawn", 30), 'HR' => array("Vicki", 41) ); Then just use a foreach () loop to fetch the keys, and check if they're in the string. If so, add the values of the current key into a temp variable for later output. Thanks, i'll do that. Could you supply a simple example of how I could use foreach() to tackle this issue? Thank you Quote Link to comment Share on other sites More sharing options...
MMDE Posted November 4, 2012 Share Posted November 4, 2012 <?php $staff = array( array("David", 31 , 'HR'), array("Julie", 29 , 'Direct Sales'), array("John", 32 , 'Admin'), array("Shawn", 30 , 'Direct Support'), array("Vicki", 41 , 'HR') ); $newStaffArray = array(); foreach($staff AS $s){ $newStaffArray[$s[2]][] = array('name'=>$s[0], 'ext'=>$s[1]); } $department = 'Admin'; $output = $department.":<br>\n"; $first = true; foreach($newStaffArray[$department] AS $person){ if($first) $first = false; else $output .= "<br>\n"; $output .= $person['name'].' on ext '.$person['ext']; } echo $output; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 4, 2012 Share Posted November 4, 2012 If you use the department as the key you can only have one person in each department unless you add another dimension to the array Quote Link to comment Share on other sites More sharing options...
dweb Posted November 4, 2012 Author Share Posted November 4, 2012 <?php $staff = array( array("David", 31 , 'HR'), array("Julie", 29 , 'Direct Sales'), array("John", 32 , 'Admin'), array("Shawn", 30 , 'Direct Support'), array("Vicki", 41 , 'HR') ); $newStaffArray = array(); foreach($staff AS $s){ $newStaffArray[$s[2]][] = array('name'=>$s[0], 'ext'=>$s[1]); } $department = 'Admin'; $output = $department.":<br>\n"; $first = true; foreach($newStaffArray[$department] AS $person){ if($first) $first = false; else $output .= "<br>\n"; $output .= $person['name'].' on ext '.$person['ext']; } echo $output; ?> Thanks very much, it kinda works, but for some reason if I replace $department = 'Admin'; with $department = 'Call the Admin or HR department'; Then rather than printing the names, it just returns that entire variable Any idea why? Thanks Quote Link to comment Share on other sites More sharing options...
MMDE Posted November 4, 2012 Share Posted November 4, 2012 (edited) Thanks very much, it kinda works, but for some reason if I replace $department = 'Admin'; with $department = 'Call the Admin or HR department'; Then rather than printing the names, it just returns that entire variable Any idea why? Thanks That's because $department is the key in the array. You only change it to the name of the department, and it's case sensitive. For example: $department = 'HR'; $department = 'Admin'; $department = 'Direct Support'; Choose one, or have an array of several and loop through them. $output is the variable you want to assign more data to be printed to screen. Try to understand what the code I gave you do. If you are unsure how the array look like, do this before the foreach: print_r($newStaffArray); Then look at the source of the HTML code to see the array/data structure well formatted for you. Array ( [hr] => Array ( [0] => Array ( [name] => David [ext] => 31 ) [1] => Array ( [name] => Vicki [ext] => 41 ) ) [Direct Sales] => Array ( [0] => Array ( [name] => Julie [ext] => 29 ) ) [Admin] => Array ( [0] => Array ( [name] => John [ext] => 32 ) ) [Direct Support] => Array ( [0] => Array ( [name] => Shawn [ext] => 30 ) ) ) Admin:<br> John on ext 32 ^ That mess is this forum. >_> Edited November 4, 2012 by MMDE Quote Link to comment Share on other sites More sharing options...
MMDE Posted November 4, 2012 Share Posted November 4, 2012 This could have been the assignment of the staff array. $staff = array( 'HR' => array( array( 'name' => 'David' 'ext' => 31 ), array( 'name' => 'Vicki' 'ext' => 41 ) ), 'Direct Sales' => array( array( 'name' => 'Julie' 'ext' => 29 ) ), 'Admin' => array( array( 'name' => 'John' 'ext' => 32 ) ), 'Direct Support' => array( array( 'name' => 'Shawn' 'ext' => 30 ) ) ) And then you can skip this part entirely: $staff = array( array("David", 31 , 'HR'), array("Julie", 29 , 'Direct Sales'), array("John", 32 , 'Admin'), array("Shawn", 30 , 'Direct Support'), array("Vicki", 41 , 'HR') ); $newStaffArray = array(); foreach($staff AS $s){ $newStaffArray[$s[2]][] = array('name'=>$s[0], 'ext'=>$s[1]); } Quote Link to comment Share on other sites More sharing options...
dweb Posted November 4, 2012 Author Share Posted November 4, 2012 I am able to change it, so if you think there's a better script to use to do this, then i'd be happy to change it Quote Link to comment Share on other sites More sharing options...
Christian F. Posted November 4, 2012 Share Posted November 4, 2012 If you only have one contact person per department, then you don't need to change it. Also, while MMDE's post related to this issue, he didn't post the code for the scenario I described in my post. For that you'll have to use this code, assuming the array is in the pattern I described: $output = array (); foreach ($staff as $dep => $contact) { if (strpos ($text, $dep)) { $output[] = vsprintf ($template, $contact); } } echo implode ("<br>\n", $output); Define $template to be the printf () template string you want to add the details to, and you're set. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 4, 2012 Share Posted November 4, 2012 here you go <?php $staff = array( array("David", 31 , 'HR'), array("Julie", 29 , 'Direct Sales'), array("John", 32 , 'Admin'), array("Shawn", 30 , 'Direct Support'), array("Vicki", 41 , 'HR') ); /******************** * rearrange the array *********************/ $depts = array(); foreach ($staff as $s) { $d = array_pop($s); $depts[$d][] = $s; } $page_text = 'If you need to contact Direct Sales or HR, please call us'; echo $page_text . '<br />' . getStaff($depts, $page_text); /************** * find departments in the text * and get the staff for those * departments **************/ function getStaff(&$darr, $t) { $res = ''; foreach ($darr as $d => $staff) { if (false !== stripos($t, $d)) { foreach ($staff as $s) { $res .= "{$s[0]} (ext {$s[1]})<br />"; } } } return $res; } ?> Quote Link to comment Share on other sites More sharing options...
dweb Posted November 4, 2012 Author Share Posted November 4, 2012 Thanks everyone, managed to finally get it working Barand, tried your code also and works a charm, thank you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.