如何在嵌套数组php中获得相同的键和不同的值

时间:2019-06-28 03:27:03

标签: php arrays

我有这篇文章,并在孩子里面得到方法。

$array1 = [
    "attribute" => "MySchool",
    "child" => 
        [
            "method" => "GET",
            "child" => [
                "attribute" => "school",
                "child" => [
                    [
                        "attribute" => "harvard"
                    ],                    
                ],
            ],
        ],
        [
            "method" => "POST",
            "child" => [
                "attribute" => "school",
                "child" => [
                    [
                        "attribute" => "stanford"
                    ],                    
                ],
            ],
        ],
]
$array2 = array(
    0 => "GET"
    1 => "school"
    2 => "harvard"
);

现在我只想要方法get及其属性值。
所以我想要像这样的数组结果:

array(
0 => "MySchool"
1 => "get"
2 => "school"
3 => "harvard"
)

4 个答案:

答案 0 :(得分:0)

如果响应始终采用相同的格式。
您可以使用以下代码格式化数组:

$result = [
    $array1["attribute"],
    $array1["child"]["method"],
    $array1["child"]["attribute"]
    $array1["child"]["child"]["attribute"]
];

答案 1 :(得分:0)

您可以检查'method'的键值是否为GET,然后提取所需的元素。

$result = [];
foreach ($array1 as $key => $value) {
    if ($key === 'attribute') $result[] = $value;
    if ($key === 'child' && $value['method'] === 'GET') {
        $result[] = $value['method'];
        $result[] = $array1['child']['child']['attribute'];
        $result[] = $array1['child']['child']['child'][0]['attribute'];
    }
}
print_r($result);
/*
 * Output: 
 * Array
 * (
 *     [0] => MySchool
 *     [1] => GET
 *     [2] => school
 *     [3] => harvard
 * )
 */

答案 2 :(得分:0)

这是您的摘录,

/**
 * Function to fetch array with type
 *
 * @param      array   $array  The array
 * @param      string  $slug   The slug
 *
 * @return     array   custom array
 */
function custom($array, $slug)
{
    $res = [];
    if (is_array($array)) {
        // default attribute I assign to result as first attribute
        $res[] = $array['attribute'];
        foreach ($array['child'] as $k => $v) {
            if ($v['method'] == $slug) {
                $res[] = strtolower($v['method']);
                array_walk_recursive($v['child'], function ($item, $k1) use (&$res) {
                    if ($k1 == 'attribute') {
                        $res[] = $item;
                    }
                });
            }
        }
    }
    return $res;
}
$slug = 'GET';
$temp = custom($array1, $slug);
print_r($temp);
$slug = 'POST';
$temp = custom($array1, $slug);

Demo

获取的输出:

Array
(
    [0] => MySchool
    [1] => get
    [2] => school
    [3] => harvard
)

发帖

Array
(
    [0] => MySchool
    [1] => post
    [2] => school
    [3] => stanford
)

答案 3 :(得分:0)

  

尝试以下操作:

 $result = getArray($array1);

function getArray($array, $new_array = [])
{
    $result = [];
    foreach ($array as $key => $value) {
        if ($key === 'attribute'
            || $key === 'method') {
            $new_array[] = $value;
        }
    }
    if (isset($array['child'])
        && is_countable($array['child'])
        && !isset($array['child'][0])
) {
        getArray($array['child'], $new_array);
    }
    elseif(isset($array['child'][0])) {
        getArray($array['child'][0], $new_array);
    } else {
        return print_r($new_array);
    }

}