0

我需要将数组中的一些键值合并到一个新的键 => 值中,但在同一个数组中,我不知道如何。

所以我有这个数组:

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
)

我需要一个这样的数组:

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

哦,如果 url_2、url_3、url_4 为空,则不要将分隔符放在 url_1 之后,当然如果 url_1 为空,则不要将分隔符放在 url_2 之前

4

5 回答 5

1

尝试这个

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

演示http://codepad.org/SwP87PyL

于 2012-09-06T01:12:17.123 回答
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);
于 2012-09-06T01:06:54.523 回答
0

也试试这个:

$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);
于 2012-09-06T01:14:53.583 回答
0

如果你确定你总是在数组的开头有这两个字段(标题,状态),你可以有类似..

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

因此,array_shift将检索并删除数组的第一个元素,通过使用它两次,您将从数组中删除标题和状态,留下 url_X 字段,然后您可以使用implode合并剩余的数组项,使用第一个参数作为胶水(参见 php 文档)。

于 2012-09-06T01:23:36.440 回答
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;
}
于 2012-09-06T01:07:17.320 回答