Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 数组\u推入现有密钥_Php_Arrays - Fatal编程技术网

Php 数组\u推入现有密钥

Php 数组\u推入现有密钥,php,arrays,Php,Arrays,所以我一直在尝试将一个数组推进到一个数组中,但它最终分别创建了一个数组。不确定如何表达,但下面是php代码: function build_embed() { $output = array( "username" => "Form Username", "description" => "Form Description", "tts&

所以我一直在尝试将一个数组推进到一个数组中,但它最终分别创建了一个数组。不确定如何表达,但下面是php代码:

function build_embed() {

    $output = array(
        "username" => "Form Username",
        "description" => "Form Description",
        "tts" => false,
        "embeds" => [
            [
                "title" => "Form Title",
                "type" => "rich",
                "fields" => []
            ]
        ]
    );

    foreach ( $this->inputs as $val ) :

        $id = $val['id'];
        $label = $val['label'];

        $newfields = [
            "name" => $label,
            "value" => $id,
            "inline" => false
        ];

        array_push($output['embeds']['fields'], $newfields);

    endforeach;

    return $output;
}
阵列的输出:

array(4) { 
    ["username"]=> string(13) "Form Username" 
    ["description"]=> string(16) "Form Description" 
    ["tts"]=> bool(false) 
    ["embeds"]=> array(2) { 
        [0]=> array(3) { 
            ["title"]=> string(10) "Form Title" 
            ["type"]=> string(4) "rich" 
            ["fields"]=> array(0) { } 
        } 
        ["fields"]=> NULL 
    } 
}
应该是:

array(4) { 
    ["username"]=> string(13) "Form Username" 
    ["description"]=> string(16) "Form Description" 
    ["tts"]=> bool(false) 
    ["embeds"]=> array(2) { 
        [0]=> array(3) { 
            ["title"]=> string(10) "Form Title" 
            ["type"]=> string(4) "rich" 
            ["fields"]=> array("name" => "Test","value" => "Test","inline" => false) { } 
        } 
    }

您已将
embedded
定义为具有一个元素数组()的多维数组,因此您将推到
embeddes
的第一个元素,即
0
,然后推到
字段

array_push($output['embeds'][0]['fields'], $newfields);
然而,这是一种更为常见和实用的方法:

$output['embeds'][0]['fields'][] = $newfields;
如果它是这样定义的,它将与当前代码一起工作:

    "embeds" => [
            "title" => "Form Title",
            "type" => "rich",
            "fields" => []
    ]
或:


既然您在循环中推动,那么对于多个输入,
字段
应该是什么?关联数组中不能有重复的键。显示的输出不能来自发布的代码。这将来自于
array\u push($output['embeddes',$newfields)
实际上,它也不是来自于这个。它来自
$output['embeds']['fields']=null从中我得到数组(4){[“用户名”]=>string(13)“表单用户名”[“描述”]=>string(16)“表单描述”[“tts”]=>bool(false)[“嵌入”]=>array(2){[0]=>array(3){[title”]=>string(10)“表单标题”[“类型”]=>string(4)“丰富”[“字段”]=>array(0{}[1]=>array(3){[“名称”=>string(8)“问题”[]value”=>string(8)“问题”[“内联”]=>bool(false)}}}}我试图将其放入字段[],将有多个输入。你需要
字段
成为一个二维数组,这样你就可以有多个
名称/值/内联
嵌套数组。你发布的代码就是这样做的。这不会产生期望的结果,它只会将关联数组进一步嵌套一层。是的,刚才我看到了一个混乱,1分钟。实际上是
array_push($output['embeddes'][0]['fields'],$newfields);
-索引紧跟在
embeddes
之后,要拾取它的第一个元素,请再次思考,属性应该是
$output['embeds'][0]['fields']=$newfields;
-因为
fields
只存储一个数组,而不是一个数组数组。不,它是在循环中添加多个数组,这样它们就可以相互覆盖。
$output['embeds']['fields'][] = $newfields;