1

I have a multidimensional array that I am trying to parse through and it is throwing back weird results (or I am just overlooking something simple). Here is a sample print_r of the json_decode from the beginning:

Array
(
    [DateGenerated] => 2014-01-12T19:30:21.897
    [Corporation] => Array
        (
            [CorporateBuilderNumber] => MHI
            [CorporateState] => TX
            [CorporateName] => McGuyer Homebuilders, Inc.
            [Builder] => Array
                (
                    [0] => Array
                        (
                            [BuilderNumber] => COV
                            [BrandName] => Coventry Homes
                            [ReportingName] => Coventry Homes
                            [DefaultLeadsEmail] => [email protected]
                            [BuilderWebsite] => http://www.coventryhomes.com
                            [Subdivision] => Array

When using this code:

<?php
mysql_connect("localhost", "", "");
mysql_select_db("");

$json_data = file_get_contents('mhi.json');
$json = json_decode($json_data, true);

foreach ($json as $value) {
    echo $value['CorporateBuilderNumber'] . "<br />";
    echo $value['CorporateState'] . "<br />";
    echo $value['CorporateName'] . "<br />";
}

?>

I get this as the output:

2
2
2
MHI
TX
McGuyer Homebuilders, Inc.

Where is that 2 coming from?

7
  • foreach ($json as $key=>$value)? Commented Jan 13, 2014 at 23:06
  • There's no problem with omitting the $key in a foreach statement. I feel like the 2's are probably coming from some earlier code. What do you have before your foreach statement? Commented Jan 13, 2014 at 23:07
  • I realised what's happening and submitted an answer below. Commented Jan 13, 2014 at 23:09
  • @Scopey: Right, I misread. Commented Jan 13, 2014 at 23:10
  • You code does nothing to try to handle this as a multi-dimensional array. I would think you would need to be looking into $value['Corporation']['CorporateBuilderNumber'], $value['Corporation']['CorporateState'], $value['Corporation']['Builder'][0]['BuilderNumber'], and so forth. Commented Jan 13, 2014 at 23:14

3 Answers 3

2

The first time in your loop, $value contains "2014-01-12T19:30:21.897".

As you are trying to access $value['CorporateBuilderNumber'] and that key does not exists, that translates to $value[0], which is the first character of the string, 2.

Sign up to request clarification or add additional context in comments.

1 Comment

This was the correct answer. I ended using XML instead and got done what I needed to accomplish. Thanks!
0

The problem is that you are looping through your 'DateGenerated' key first.

All your strings from

echo $value['CorporateBuilderNumber'] . "<br />";
echo $value['CorporateState'] . "<br />";
echo $value['CorporateName'] . "<br />";

are being converted into int(0), and are referencing the first character of the date you have there, which is a 2.

Comments

0

You can run your table with this :

foreach ($json as $key => $value) {
    if (is_array($value) ){
        foreach( $value as $key2 => $value2){
            if( is_array($values2) ) {
                foreach( $value2 as $key3 => $value3){
                    echo $value3. "<br />";
                }       
            } 
            else{
                echo $value2. "<br />";
            }
        }
    }
    else {
        echo $value. "<br />";
    }
}

2 Comments

OK I see what you did there.
You can continue with more foreach because i saw that there is a fourth array in the sample of your prrint_r

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.