Jump to content

Looking Up Words


dweb

Recommended Posts

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 by dweb
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites


<?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;
?>

Link to comment
Share on other sites


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

Link to comment
Share on other sites

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 by MMDE
Link to comment
Share on other sites

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]);
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.