Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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,我的项目中有这个数组,我想将它们合并为一维数组 "results": [ { "code": "jne", "name": "Jalur Nugraha Ekakurir (JNE)", "costs": [ { "service": "OKE", "description": "Ongkos Ki

我的项目中有这个数组,我想将它们合并为一维数组

"results": [
        {
            "code": "jne",
            "name": "Jalur Nugraha Ekakurir (JNE)",
            "costs": [
                {
                    "service": "OKE",
                    "description": "Ongkos Kirim Ekonomis",
                    "cost": [
                        {
                            "value": 50000,
                            "etd": "4-5 HARI",
                            "note": ""
                        }
                    ]
                },
                {
                    "service": "REG",
                    "description": "Layanan Reguler",
                    "cost": [
                        {
                            "value": 55000,
                            "etd": "2-3 HARI",
                            "note": ""
                        }
                    ]
                },
                {
                    "service": "YES",
                    "description": "Yakin Esok Sampai",
                    "cost": [
                        {
                            "value": 145000,
                            "etd": "1-1 HARI",
                            "note": ""
                        }
                    ]
                }
            ]
        }
    ]
如果您将此结果制成表格,我希望它如下所示:


有人能帮我解决这个问题吗?

很抱歉,查看您的阵列,这是不可能的<代码>数组键只能是唯一的,在您的情况下,需要具有多维数组。检查嵌套数组
成本
。您将看到每个数组位置都有相同的键名。所以不可能把这个数组变成一维

我建议这样做是为了帮助您更好地理解阵列。

以下是

下面是代码片段

<?php


$json_string = <<<EOF
{

"results": [
        {
            "code": "jne",
            "name": "Jalur Nugraha Ekakurir (JNE)",
            "costs": [
                {
                    "service": "OKE",
                    "description": "Ongkos Kirim Ekonomis",
                    "cost": [
                        {
                            "value": 50000,
                            "etd": "4-5 HARI",
                            "note": ""
                        }
                    ]
                },
                {
                    "service": "REG",
                    "description": "Layanan Reguler",
                    "cost": [
                        {
                            "value": 55000,
                            "etd": "2-3 HARI",
                            "note": ""
                        }
                    ]
                },
                {
                    "service": "YES",
                    "description": "Yakin Esok Sampai",
                    "cost": [
                        {
                            "value": 145000,
                            "etd": "1-1 HARI",
                            "note": ""
                        }
                    ]
                }
            ]
        }
    ]

}
EOF;


// json string to php array
$array = json_decode($json_string, true);

// output array
$output = array();

// loop through array
foreach($array['results'] as $item)
{
   $main_fields = array( 'code' => $item['code'], 'name' => $item['name'] );
   foreach($item['costs'] as $sub ){
          $t = $sub['cost'][0];
              unset($sub['cost']);
              $tmp = array_merge($main_fields, $sub, $t);
              $output[] = $tmp;  
   } 

}

// print contents
print_r($output);


// to get html table 
// iterate array $output or else inside above loop you can do it

echo "<table>\n";
echo "<tr>\n<th>".implode("</th>\n<th>", array_keys($output[0]))."</th>\n</tr>\n";
foreach($output as $row){
   echo "<tr>\n<td>".implode("</td>\n<td>", $row)."</td>\n</tr>\n";
}
echo "</table>"

?>

将产生:

Array
(
    [0] => Array
        (
            [code] => jne
            [name] => Jalur Nugraha Ekakurir (JNE)
            [service] => OKE
            [description] => Ongkos Kirim Ekonomis
            [value] => 50000
            [etd] => 4-5 HARI
            [note] => 
        )

    [1] => Array
        (
            [code] => jne
            [name] => Jalur Nugraha Ekakurir (JNE)
            [service] => REG
            [description] => Layanan Reguler
            [value] => 55000
            [etd] => 2-3 HARI
            [note] => 
        )

    [2] => Array
        (
            [code] => jne
            [name] => Jalur Nugraha Ekakurir (JNE)
            [service] => YES
            [description] => Yakin Esok Sampai
            [value] => 145000
            [etd] => 1-1 HARI
            [note] => 
        )

)
<table>
<tr>
<th>code</th>
<th>name</th>
<th>service</th>
<th>description</th>
<th>value</th>
<th>etd</th>
<th>note</th>
</tr>
<tr>
<td>jne</td>
<td>Jalur Nugraha Ekakurir (JNE)</td>
<td>OKE</td>
<td>Ongkos Kirim Ekonomis</td>
<td>50000</td>
<td>4-5 HARI</td>
<td></td>
</tr>
<tr>
<td>jne</td>
<td>Jalur Nugraha Ekakurir (JNE)</td>
<td>REG</td>
<td>Layanan Reguler</td>
<td>55000</td>
<td>2-3 HARI</td>
<td></td>
</tr>
<tr>
<td>jne</td>
<td>Jalur Nugraha Ekakurir (JNE)</td>
<td>YES</td>
<td>Yakin Esok Sampai</td>
<td>145000</td>
<td>1-1 HARI</td>
<td></td>
</tr>
</table>
数组
(
[0]=>阵列
(
[代码]=>jne
[姓名]=>新的生活方式(JNE)
[服务]=>OK
[说明]=>Ongkos Kirim Ekonomis
[值]=>50000
[etd]=>4-5哈里
[注]=>
)
[1] =>阵列
(
[代码]=>jne
[姓名]=>新的生活方式(JNE)
[服务]=>REG
[说明]=>Layanan Reguler
[值]=>55000
[etd]=>2-3哈里
[注]=>
)
[2] =>阵列
(
[代码]=>jne
[姓名]=>新的生活方式(JNE)
[服务]=>是
[说明]=>雅金·埃索克山派
[值]=>145000
[etd]=>1-1哈里
[注]=>
)
)
代码
名称
服务
描述
价值
etd
笔记
jne
新喀里尔(JNE)
奥克
Ongkos Kirim Ekonomis
50000
4-5哈里
jne
新喀里尔(JNE)
规则
雷亚南调节器
55000
2-3哈里
jne
新喀里尔(JNE)
对
雅金·埃索克·桑帕伊
145000
1-1哈里

您可以使用循环:

$result = array();
foreach ($myArray["results"] as $item) {
    $name = $item["name"];
    $code = str_replace(")", "", str_replace("(", "", substr($name, strpos($name, "("))));
    $name = trim(substr($name, 0, strpos($name, "(") - 1));
    foreach ($item["costs"] as $cost) {
        $service = $cost["service"];
        $description = $cost["description"];
        foreach ($cost["cost"] as $costItem) {
            $result[]= array(
                "code" => $code,
                "name" => $name,
                "service" => $service,
                "description" => $description,
                "value" => $costItem["value"],
                "edt" => $costItem["etd"],
                "note" => $costItem["note"]
            );
        }
    }
}

你应该包括到目前为止你已经尝试过的内容,或者如果你根本没有尝试过任何东西,那么就尽可能地尝试,如果你很笨,那么就回到这里谢谢你的代码我可以让它成为一维数组你的代码非常有用谢谢你非常感谢,实际上,这是我第一次问堆栈溢出问题,只是要提到,这仍然被认为是多维数组,而不是一维数组。为了访问它的值,您需要围绕它进行循环。除此之外,这是一个正确的解决方案。谢谢你的建议,我会再次阅读,这样我就可以成为像你们一样的优秀开发人员。谢谢你们的帮助。谢谢你们有用的代码。现在我可以完成我的工作了。你们是best@Euclides不客气!如果它解决了你的问题,你可以考虑接受一个答案。