Jump to content

Manipulate Array Output


unemployment

Recommended Posts

I have an array and I would like to insert commas and the word 'and' to make a sentence using an array.

 

For example...

 

$sports = array(baseball, soccer, tennis);

 

Ted played baseball, soccer and tennis

 

or

 

Ted played baseball, soccer, boxing and tennis

 

What's the best way to do this?

Link to comment
Share on other sites

Also, I want to combine two arrays so that they array keys match. For example...

 

$sports = array(soccer, basketball, tennis);

$locations = array(New York, London, Paris);

 

Ted plays soccer in New York, Basketball in London and Tennis in Paris.

 

How do you compare arrays like that to create this sentence structure?

Link to comment
Share on other sites

one way is

 

$sports = array(baseball, soccer, tennis);

$str = implode(",",$sports);

$pos = strrpos($str,',');

$final_str = substr($str,0,$pos).' and '.substr($str,($pos+1),strlen($str));

echo $final_str;

 

there may be other ways also...

 

 

Link to comment
Share on other sites

to match the arrays by key, you could use array_merge_recursive()

 

I tried working with you suggestion, but I just can't seem to get it to work the way I need it to.

 

This is a print out of two of my arrays

Array
(
    [0] => stageofdevelopment
    [1] => companylocation
)
Array
(
    [0] => 
    [1] => New City, South Africa
)

 

This is a print out of the merge

Array
(
    [0] => stageofdevelopment
    [1] => companylocation
    [2] => 
    [3] => New City, South Africa
)

This is the function I used for the merge

 

function my_array_merge ($arr,$ins) {
    if(is_array($arr))
    {
        if(is_array($ins)) foreach($ins as $k=>$v)
        {
            if(isset($arr[$k])&&is_array($v)&&is_array($arr[$k]))
            {
                $arr[$k] = my_array_merge($arr[$k],$v);
            }
            else {
                // This is the new loop 
                while (isset($arr[$k]))
                    $k++;
                $arr[$k] = $v;
            }
        }
    }
    elseif(!is_array($arr)&&(strlen($arr)==0||$arr==0))
    {
        $arr=$ins;
    }
    return($arr);
}

 

This is what I want...or at least what I think I want.

 

Array
(
    [0] => Array
        (
            [0] => stageofdevelopment
            [1] => 
        )

    [1] => Array
        (
            [0] => companylocation
            [1] => New City, South Africa
        )
)

 

Please help...

Link to comment
Share on other sites

Also for the previous problem

$common1=array_combine($sports,$location);

function print_arr($common1)

{

$final_str='';

$callback =function($value,$key) use (&$final_str)

{

$final_str .= $key.' in '.$value.',';

};

array_walk($common1,$callback);

return $final_str;

}

$final_output=print_arr($common1);

echo $final_output;

 

you may need to tweak a little to append 'and'

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.