...

View Full Version : array manipulation



ecnarongi
09-23-2004, 05:50 AM
I have this routine:

<?php

function splitMe2(&$arrs)
{
extract($arrs,EXTR_REFS);
$arrs[0].$arrs[1].$arrs[2];
}

$newcart = array(array('Tim', 'Donald', 'Roy'),
array('John', 'Ronald', 'Troy'),
array('Mary', 'Kate', 'Martha'));

echo "Hello, " .array_walk(&$newcart, 'splitMe2'). " welcome to todays class.";
?>

I want it to ouput:
"Hello, Tim Donald Roy John Ronald Troy Mary Kate Martha welcome to todays class"

Can anyone help me get the desired results. All help is appreciated.

mordred
09-23-2004, 08:44 AM
Here's an example on how to achieve the result by using array_map() and implode():



function combine($arr) {
return implode(' ', $arr);
}

$newcart = array(array('Tim', 'Donald', 'Roy'),
array('John', 'Ronald', 'Troy'),
array('Mary', 'Kate', 'Martha'));

echo "Hello, " . implode(' ', array_map('combine', $newcart)). " welcome to todays class.";

ecnarongi
09-24-2004, 02:02 AM
Thanks :thumbsup: this worked exactly the way I wanted it to.

Ok, I have a new issue what if I ony wanted to output array[0] and array[2]?
To have an output of:

"Hello, Tim Roy John Troy Mary Martha welcome to todays class"

Secondly, what if I wanted to Append "Johnson" to everyone's name in the function not in the echo statement, how would I achieve this?

All help is appreciated.

ecnarongi
09-24-2004, 04:45 AM
I actually answered my own question with




function combine($arr) {
extract($arr,EXTR_REFS);
return $arr[0]." Johnson ".$arr[1]." Johnson ".$arr[2]." Johnson";
}

$newcart = array(array('Tim', 'Donald', 'Roy'),
array('John', 'Ronald', 'Troy'),
array('Mary', 'Kate', 'Martha'));

echo "Hello, " . implode(' ', array_map('combine', $newcart)). " welcome to todays class.";


thanks again



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum