Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP重新排序值_Php_Arrays_Multidimensional Array - Fatal编程技术网

PHP重新排序值

PHP重新排序值,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个如下所示的数组: $content = array(array("id_post" => 1000, "id_user" => 4), array("id_post" => 1001, "id_user" => 4), array("id_post" => 1002, "id_user" => 3), array("id_post" => 100

我有一个如下所示的数组:

$content = array(array("id_post" => 1000, "id_user" => 4),
                 array("id_post" => 1001, "id_user" => 4),
                 array("id_post" => 1002, "id_user" => 3),
                 array("id_post" => 1003, "id_user" => 4),
                 array("id_post" => 1004, "id_user" => 5),
                 array("id_post" => 1005, "id_user" => 5));
所以它的意思是
5=>10041005
4=>1000100110003
3=>1002

首先,我如何得到这个结构?(可以使用逗号)

这个解决方案的算法是这样的(这是我要问你们的……如何做到这一点):


我认为您最好还是坚持使用数组,而不是使用逗号连接,然后在以后进行分解:

foreach($content as $values) {
    if(!isset($result[$values['id_user']])) {
        $result[$values['id_user']] = array();
    }
    array_push($result[$values['id_user']], $values['id_post']);
}
print_r($result);
试试这个:

$arr = array();
// Loop through each content
foreach ($content as $post)
{
  $arr[$post['id_user']][] = $post['id_post'];
}
这样,结果将是

$arr = array(
  '5'=> array('1004', '1005'),
  '4'=> array('1000', '1001', '1003'),
  '3'=> array('1002')
);
这样,您就不需要使用“explode”来拆分这些逗号分隔的ID
此外,这种变体可能正是您所需要的:

$content = array(array("id_post" => 1000, "id_user" => 4),
    array("id_post" => 1002, "id_user" => 3),
    array("id_post" => 1004, "id_user" => 5),
    array("id_post" => 1003, "id_user" => 4),
    array("id_post" => 1001, "id_user" => 4),
    array("id_post" => 1005, "id_user" => 5));

// Make preparation array
foreach ( $content as $values ) {
    if ( !isset($result[$values['id_user']]) ) {
        $result[$values['id_user']] = array();
    }
    array_push($result[$values['id_user']], $values['id_post']);
}

// Sort inner arrays
foreach ( $result as $key => $valueArray ) {
    asort($result[$key]);
}

// Implode arrays to strings
array_walk($result, function(&$array, $key) {
            $array = implode(',', $array);
        });

// Final array
$result1 = array();
foreach ( $result as $userID => $idsString ) {
    $result1[] = array("id_user" => $userID, "id_post" => $idsString);
}

print_r($result1);

很 完美。只需完成回答,即可访问这些值:
foreach($arr as$key=>$post){echo$key.“
”;}
$arr = array(
  '5'=> array('1004', '1005'),
  '4'=> array('1000', '1001', '1003'),
  '3'=> array('1002')
);
$content = array(array("id_post" => 1000, "id_user" => 4),
    array("id_post" => 1002, "id_user" => 3),
    array("id_post" => 1004, "id_user" => 5),
    array("id_post" => 1003, "id_user" => 4),
    array("id_post" => 1001, "id_user" => 4),
    array("id_post" => 1005, "id_user" => 5));

// Make preparation array
foreach ( $content as $values ) {
    if ( !isset($result[$values['id_user']]) ) {
        $result[$values['id_user']] = array();
    }
    array_push($result[$values['id_user']], $values['id_post']);
}

// Sort inner arrays
foreach ( $result as $key => $valueArray ) {
    asort($result[$key]);
}

// Implode arrays to strings
array_walk($result, function(&$array, $key) {
            $array = implode(',', $array);
        });

// Final array
$result1 = array();
foreach ( $result as $userID => $idsString ) {
    $result1[] = array("id_user" => $userID, "id_post" => $idsString);
}

print_r($result1);