douhui5953 2015-06-23 18:39
浏览 37

在php多维中格式化单个结果以匹配?

I'm grabbing a some XML data and parsing it with PHP.

Most of the results come in multidimensional array but occasionally I'll get a result in a single array and it breaks my script.

I'm trying to format the single result to match the format of the results in the multidiminsonal array but I'm not having any luck.

Here is what I got:

    Array
    (
                [name] => Steve Jobs
                [id] => 3
    )

Array
    (
        [0] => Array
            (
                [name] => Steve Jobs
                [id] => 6
            )

        [1] => Array
            (
                [name] => Bill Gates
                [id] => 8
            )

    )

I'm trying to format the single result to match the multidimensional format then flatten...

Array
    (
        [0] => Array
            (
                [name] => Steve Jobs
                [id] => 3
            )

        [1] => Array
            (
                [name] => Steve Jobs
                [id] => 6
            )

        [2] => Array
            (
                [name] => Bill Gates
                [id] => 8
            )

    )

I've tried this:

$array_check = @array_keys($result[0]['name']);

if ($array_check[0] == "0") {
            $result;

        } elseif ($array_check[0] == "name") {
            $ReWrite =  array ([0] =>
                        array (['name']=>
                        array ($result[0]['name'])
                        ));

            $result = $ReWrite;

        }

I thought that would do it but it's off...

  • 写回答

2条回答 默认 最新

  • dourukeng5302 2015-06-23 18:47
    关注

    Try this:

        $array_check = @array_keys($result[0]['name']);
    
        if (!isset($array_check[0])) {
            $result[] = $array_check;
        } else {
            $result = $array_check;
        }
    
        var_dump($result);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器