简体   繁体   中英

Push same key to associative array in 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>

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"));
        }
     }
}

I solved this by using:

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

Which would output

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

And editing my array_to_xml() function to change numeric keys to the string 'line' which produces the right XML

private function array_to_xml($entries, &$tmpXML) {
        foreach ( $entries as $key => $value ) {
            if (is_array ( $value )) {
                if (! is_numeric ( $key )) {
                    $subnode = $tmpXML->addChild ( "$key" );
                    $this->array_to_xml ( $value, $subnode );
                } else {
                    $subnode = $tmpXML->addChild ( "line" );
                    $this->array_to_xml ( $value, $subnode );
                }
            } else {
                $tmpXML->addChild ( "$key", htmlspecialchars ( "$value" ) );
            }
        }
    }

Thanks for everyone's input

You can't have the same keys but you can have :

EDIT 1 :

array(
        [0] => array(
           'line' => array(
              // Ean-artikelcode
              'Article_Eancode' => 8710624618216,
              // Leveranciersartikelcode
              'Article_Supplier_Partno' => 22304
        )),
        [1] => array(
          'line' => array(
             'Article_Eancode' => 8710622648216,
             'Article_Supplier_Partno' => 22304
        ))
   );

And you can do it like this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM