dongle0396 2015-08-27 12:58
浏览 39
已采纳

在PHP中将相同的键推送到关联数组

I'm trying to add arrays to an associative array in PHP. I know this isn't how you're supposed to use the keys but I'm parsing the array to XML which needs te same <line> tag.

Desired array:

array(
    'line' => array(
        // Ean-artikelcode
        'Article_Eancode' => 8710624618216,
        // Leveranciersartikelcode
        'Article_Supplier_Partno' => 22304
    ),

    'line' => array(
        'Article_Eancode' => 8710622648216,
        'Article_Supplier_Partno' => 22304
    )
);

Which I am trying to get with this code:

$artikelenFormatted = array();
$artikelen          = array(
    'a',
    'b',
    'c'
);
foreach ($artikelen as $art) {
    $artikelenFormatted['line'] = array(
        "Article_Eancode" => "a",
        "Article_Supplier_Partno" => "b"
    );
}

Which produces:

array (size=1)
  'line' => 
    array (size=2)
      'Article_Eancode' => string 'a' (length=1)
      'Article_Supplier_Partno' => string 'b' (length=1)

Because $array['line'] keeps getting overwritten so there aren't multiple entries

How would I do this?

EDIT: Sample of the desired XML

<Lines>
  <Line>
    <Article_Eancode>87XXXXXXXXXXX</Article_Eancode>
    <Article_Supplier_Partno>22304</Article_Supplier_Partno>
  </Line>
  <Line>
    <Article_Eancode>87XXXXXXXXXXX</Article_Eancode>
    <Article_Supplier_Partno>22303</Article_Supplier_Partno>
  </Line>
  <Line>
    <Article_Eancode>87XXXXXXXXXXX</Article_Eancode>
    <Article_Supplier_Partno>22324</Article_Supplier_Partno>
  </Line>
  <Line>
    <Article_Eancode>87XXXXXXXXXXX</Article_Eancode>
    <Article_Supplier_Partno>22305</Article_Supplier_Partno>
  </Line>
  <Line>
    <Article_Eancode>87XXXXXXXXXXX</Article_Eancode>
    <Article_Supplier_Partno>22323</Article_Supplier_Partno>
  </Line>
</Lines>
  • 写回答

3条回答 默认 最新

  • dongliehuan3925 2015-08-27 13:03
    关注

    An array cannot have two (or more) of the same key. Consider; what would $array['line'] return?

    What you're looking for is:

    foreach ($artikelen as $art) {
        $artikelenFormatted['line'][] = array(
            "Article_Eancode" => "a",
            "Article_Supplier_Partno" => "b"
        );
    }
    

    Notice the [] after ['line']. This will make $artikelenFormatted['line'] an array where each element is an array of the data.

    Edit:

    To get it to work with XML, use the following:

    foreach ($artikelen as $art) {
        $artikelenFormatted[]['line'] = array(
            "Article_Eancode" => "a",
            "Article_Supplier_Partno" => "b"
        );
    }
    

    And amend the array_to_xml function you reference to:

    function new_array_to_xml( $data, &$xml_data ) {
        foreach( $data as $key => $value ) {
            if( is_array($value) ) {
                if( is_numeric($key) ){
                array_to_xml($value, $xml_data);
                }
                else
                {
                $subnode = $xml_data->addChild($key);
                array_to_xml($value, $subnode);
                }
            } else {
                $xml_data->addChild("$key",htmlspecialchars("$value"));
            }
         }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制