0

I'm having some trouble understanding the coding in PHP, when it comes to multidimensional arrays and how to push. The idea is to push a "Attribute" and a "Attribute value"

I have tried the formula below

   $i = 0;
   $array = array();
    foreach($node as $a)
    {
        $strAtt = $node->PROP[$i]->attributes();
        $strVal = $node->PROP[$i]->PVAL;

        $output = $output.$strAtt." : ".$strVal."<BR>";
        $array[] = ($strAtt => $strVal);

The $array[] = ($strAtt => $strVal); doesnt give me much success. I have tried array_push($array, $strAtt => $strVal) - no luck..

As an extra questions, how do I loop trough the array and print me multidimensional values ?.

NEW CODE

while ($z->name === 'RECORD')
{

$node = new SimpleXMLElement($z->readOuterXML());

$Print = FALSE;
$output = "";
$i = 0;
foreach($node as $a)
{
    $strAtt = $node->PROP[$i]->attributes();
    $strVal = $node->PROP[$i]->PVAL;

    $output = $output.$strAtt." : ".$strVal."<BR>";
    $array[$strAtt] = $strVal;

    if(($i == 6) && ($node->PROP[$i]->PVAL == $ProductLookup))
    {
        $Print = TRUE;
        $Product = $node->PROP[$i]->PVAL;
    }       

    $i++;
}
if($Print == TRUE) {
    echo $output;
    echo "Product : ".$Product."<br>";
    var_dump($array);
    }

    //print_r($array);
    $print = FALSE;

// go to next <product />
$z->next('RECORD');
}

New code added. For some reason my $array is totally empty when i dump it, although my $Output is full of text ?

2
  • array_push( $array, array( $strAtt => $strVal )); Commented Oct 6, 2012 at 10:48
  • You can go through this answer to understand how array works in php Commented Oct 6, 2012 at 11:08

2 Answers 2

2

It sounds like you are wanting an "associative" array and not necessarily a multi-dimensional array. For associative arrays you don't use array_push. Just do this:

$array[$strAtt] = $strVal;

Then to loop the array just do this:

foreach ($array as $key => $value) {
    echo "$key = $value\n";
}
Sign up to request clarification or add additional context in comments.

4 Comments

I see you point, but will this add the next attribute and value to my array, or do i need something like $array = $array[$strAtt] = $strVal; ?
Thats right, within the loop you can assign the key and key value with something like $newArray[$key] = $value - in this example it outputs the value on each loop.
Hmnn...I have added my "new" code. For some reason my array is empty, although lots of text in the $Output variable...Any ideas ?
@UlrikVadstrup, I see your new code. Where in your code do you initialize your $array variable? You should have a place outside the while loop where you initialize the array with $array = array();
0

Go through array in php , you will understand how array works in php. Besides if you want to add an element to a multidimensional array you can achieve like this :

$node = array ("key1"=> array (a,b) , "key2"=> array (c,d));
$array = array();
foreach ($node as $key=>$value) {
    $array [$key] = $value;
}

This will be the resulting $array after the loop :

array (
"key1"=> array (
a,b
) , 
"key2"=> 
array (c,d)
)

Hope that helps , happy coding :)

2 Comments

I think davidethell is on the right path. Maybe what i'm looking for is a "associative" array. I haven't solved my problem adding yet - though
@UlrikVadstrup , btw, this is also an associative array :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.