0

I need to merge some key values in an array into a new key => value but in the same array and I couldn't figure out how.

so I have this array :

array(
[title] => something
[status] => something
[url_1] => http://someurl.com
[url_2] => http://someurl.com
[url_3] => http://someurl.com
[url_4] => http://someurl.com
)

and I need an array like this:

array(
[title] => something
[status] => something
[all_url] => http://someurl.com,http://someurl.com,http://someurl.com,http://someurl.com
)

oh and if url_2, url_3, url_4 is empty than do not put the separators after url_1 and of course if url_1 is empty do not put the separator before url_2

1
  • What about plain old foreach + if? Commented Sep 6, 2012 at 1:04

5 Answers 5

1

You can use the implode function. If you have the url parts as another array, you can easily create the string like this:

$all_url = implode(',', $url_parts);

The implode funciton only adds the separator where needed. If the url parts are always going to be a part of the main array, you can do something like this:

$temp_arr = array();
$i = 1;
while($temp_url = $array[url_{$i}]){
    $temp_arr[] = $temp_url;
    unset($array[url_{$i}]);
    $i++;
}
$array[all_url] = implode(',', $temp_arr);
Sign up to request clarification or add additional context in comments.

1 Comment

I like that your solution is scalable. However, I would edit to incldue title and status.
1

try this

<?php
$param = array(
'title' => 'something',
'status' => 'something',
'url_1'=> 'http://someurl.com',
'url_2' => 'http://someurl.com',
'url_3' => 'http://someurl.com',
'url_4' => 'http://someurl.com',
);

$meta = array('title','status');

$metas = array_intersect_key(array_flip($meta),$param);
$metas['all_url'] = implode(',', array_diff_key($param, array_flip($meta)));

var_export($metas);

demo http://codepad.org/SwP87PyL

1 Comment

Although my answer is easier to understand what is going on, I would probably use a solution similar to this, where I could configure the fields that you don't want to join (implode).
0
$yourArray['all_url'] = implode(',', array(
    $yourArray['url_1'],
    $yourArray['url_2'],
    $yourArray['url_3'],
    $yourArray['url_4'],
));
unset($yourArray['url_1'], $yourArray['url_2'], $yourArray['url_3'], $yourArray['url_4'])

var_dump($yourArray):

Or if you want to make it more scalable:

function mergeUrls($inputArray, $filter = 'url_')
{
    $urls = array();
    foreach($inputArray as $key => $value) {
        if (strpos($value, $filter) !== 0) {
            continue;
        }

        $urls[] = $value;
        unset($inputArray[$key]);
    }

    $inputArray['all_urls'] = implode(',', $urls);
    return $inputArray;
}

3 Comments

if I simply implode it than it will write the separator even if the key doesn't have value an I will get something like this all_url=>someurl,,, or all_url=>,,someurl,
In that case implement an extra check to see whether the item contains a value.
Sorry I'm really tired but if I do a check It works fine. Thanks!
0

Also try this:

$data = array(
            'title' => 'something',
            'status' => 'something',
            'url_1' => 'http://someurl.com',
            'url_2' => 'http://someurl.com',
            'url_3' => 'http://someurl.com',
            'url_4' => 'http://someurl.com',
        );
        $array1 = array();
        $string = "";
        foreach ($data as $key => $value)
            if (substr($key, 0, 4) == "url_")
                $string = $string . $value . ",";
            else
                $array1[$key] = $value;

        $array1['all_url'] = substr($string, 0, strlen($string) - 1);
        print_r($array1);

Comments

0

If you're sure that you'll always have those 2 fields on the beginning of the array (title, status), you can have something like..

<?php
// Consider you have an array similar to this
$yourArray = array(
    'title'  => 'something',
    'status' => 'something',
    'url_1'  => 'http://someurl.com',
    'url_2'  => 'http://someurl.com',
    'url_3'  => 'http://someurl.com'
);

$newArray['title']  = array_shift($yourArray);
$newArray['status'] = array_shift($yourArray);

// Now $yourArray should only contain the url_X fields so...

$newArray['all_url'] = implode(',', $yourArray);

So, array_shift will retrieve and remove the first element of an array, by using it twice, you're removing title and status from the array, leaving you with the url_X fields, and then you can use implode to merge the remaining array items, using the first parameter as glue (see php documentation).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.