【问题标题】:Add array to array without key IDs? - PHP将数组添加到没有键 ID 的数组? - PHP
【发布时间】:2017-04-13 11:29:38
【问题描述】:

我有一个大数组,如下所示:

array(2) {
  ["Final Fantasy VII"]=>
  array(5) {
    ["rows"]=>
    array(2) {
      [0]=>
      array(6) {
        ["price"]=>
        string(5) "11.69"
        ["price_old"]=>
        string(4) "4.66"
        ["currency"]=>
        string(4) "euro"
        ["portal"]=>
        string(0) ""
        ["link"]=>
        string(77) "https://de.gamesplanet.com/game/final-fantasy-vii-download--1001-1?ref=gmkeys"
        ["shop"]=>
        string(4) "9507"
      }
      [1]=>
      array(6) {
        ["price"]=>
        string(5) "14.99"
        ["price_old"]=>
        ...
      }
    }
  }

 ["Battlefield 1"]=>
  array(3) {
    ["rows"]=>
    array(2) {
      [0]=>
      array(6) {
        ["price"]=>
        ...
      }
      [1]=>
      array(6) {
        ["price"]=>
        ...
      }
    }
  }
}

我只想获取该数组中名称与我搜索的标题匹配的某些部分。所以,我用这个代码:

function createACFRepeater($title){
    $repeater = array();

    if(searchForGameXML($title)){

        $count = count($GLOBALS["productsXML"][$title]['rows']);

        for($i = 0; $i < $count; $i++){
            array_push($repeater, $GLOBALS["productsXML"][$title]['rows'][$i]);
        }

        return $repeater;
    }else{
        return $repeater;
    }
}

我现在的问题是 $repeater 数组看起来像这样:

array(2) {
  [0]=>
  array(6) {
    ["price"]=>
    string(5) "19.98"
    ["price_old"]=>
    ...
  }
  [1]=>
  array(6) {
    ["price"]=>
    string(4) "7.99"
    ["price_old"]=>
    ...
  }
}

有一个数字键指向数组[0] =&gt; ...。但我想要的只是没有任何关联关系的数组中的一个数组...

如何创建一个如下所示的数组:?

array(2) {
      array(6) {
        ["price"]=>
        string(5) "19.98"
        ["price_old"]=>
        ...
      }
      array(6) {
        ["price"]=>
        string(4) "7.99"
        ["price_old"]=>
        ...
      }
    }

您好,谢谢!

【问题讨论】:

    标签: php arrays multidimensional-array associative-array


    【解决方案1】:

    根据array definition 是不可能的。任何数组项都必须有keyvalue,数组的文档开始于:

    PHP 中的数组实际上是一个有序映射。映射是将值与键相关联的类型

    【讨论】:

    • 我需要这个数组作为插件。在这里观看:advancedcustomfields.com/resources/update_field。在那里,数组看起来像这样:$value = array( array( "sub_field_1" =&gt; "Foo1", "sub_field_2" =&gt; "Bar1", "acf_fc_layout" =&gt; "layout_1_name" ), array( "sub_field_x" =&gt; "Foo2", "sub_field_y" =&gt; "Bar2", "acf_fc_layout" =&gt; "layout_2_name" ) ); 但也许你也有这个答案? support.advancedcustomfields.com/forums/topic/…
    • 对!但是如果你写$value[0] 你会收到`array("sub_field_1" => "Foo", "sub_field_2" => "Bar")`。如果您省略索引,它们会自动递增。这并不意味着数组根本没有索引
    • 好的,谢谢! - 我对 ACF 插件的文档感到困惑
    • 我改了。由于这些示例,我更容易阅读......您对 ACF 支持链接有什么想法吗?
    • @lubart 您的链接无法正常工作,您在链接末尾添加了http:// :)
    【解决方案2】:

    您将始终拥有数字键。正如@lubart 已经说过的那样:没有键的数组是不可能的。顺便说一句,以下所有数组都是完全相等的:

    $array1 = array([0] => array([0] => 'hi', [1] => array([0] => '23.5')));
    
    $array2 = array(array('hi', array('23.5')));
    
    $array3 = [['hi', ['23.5']]];
    
    $array4 = [ [0] => [ [0] => 'hi', [1] => [ [0] => '23.5' ] ] ];
    

    【讨论】:

      猜你喜欢
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多