0

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>
Martijn Nosyncerror
  • 172
  • 1
  • 3
  • 16
  • 2
    You cannot have 2 keys at the same level with the same name!! RE: You desired array – RiggsFolly Aug 27 '15 at 12:59
  • When I declare the array manual c.q. I just use my desired static array and pass it to my array to XML function it works perfectly. The troubles start when I want to use the for loop to push – Martijn Nosyncerror Aug 27 '15 at 13:01
  • Suggestion: Create the XML then parse it back to a PHP array, see what it looks like and then reverse engineer it back so you can create the correct structure in PHP – RiggsFolly Aug 27 '15 at 13:02
  • I know what the XML should look like, that's why I know that the array should be structured that way – Martijn Nosyncerror Aug 27 '15 at 13:04
  • _When I declare the array manual c.q. I just use my desired static array and pass it to my array to XML function it works perfectly._ You mean perfectly if I ignore that the second `line` overwrites the first `line` dont you. ***Not my definition of perfect*** – RiggsFolly Aug 27 '15 at 13:07
  • Do you have a sample of the XML we could look at? – RiggsFolly Aug 27 '15 at 13:08
  • When I pass it to [this function](http://stackoverflow.com/a/5965940/1572635) it works the way I desire, even with the duplicate keys – Martijn Nosyncerror Aug 27 '15 at 13:09
  • I put a snippit of the XML in my OP – Martijn Nosyncerror Aug 27 '15 at 13:12

3 Answers3

2

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"));
        }
     }
}
Community
  • 1
  • 1
Michael
  • 11,912
  • 6
  • 49
  • 64
1

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

Martijn Nosyncerror
  • 172
  • 1
  • 3
  • 16
0

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"
    );
   }
Lindus
  • 66
  • 5
  • That was my first thought too, until I read this bit of his question, _but I'm parsing the array to XML which needs te same tag._ – RiggsFolly Aug 27 '15 at 13:04