Jump to content

How to access a multiarray with PHP.


tommytx

Recommended Posts

This is php programming..
I have tried just about every variation i can think of.. The goal is to be able to pull each value..
Then of course once i know what i need to pull the values i can put it in a loop to get them all.
Any assistance offered will be greatly appreciated.

print "<pre>";
print_r($myarray); // multiple triple array..
print "<pre>";

Array
(
    [DATA] => Array
        (
            [0] => Array
                (
                    [CurrentPrice] => 135000.00
                    [MLSNumber] => 857252
                    [Matrix_Unique_ID] => 8971579
                    [StreetName] => DENVER
                )

            [1] => Array
                (
                    [CurrentPrice] => 135000.00
                    [MLSNumber] => 857251
                    [Matrix_Unique_ID] => 8971581
                    [StreetName] => DENVER
                )

            [2] => Array
(
[CurrentPrice] => 135000.00
[MLSNumber] => 857253
[Matrix_Unique_ID] => 8971583
[StreetName] => DENVER
)

)
     
echo [0]['MLSNumber'];

echo [0][0]['MLSNumber'];

echo $myarray['MLSNumber'];

echo $myarray[0]['MLSNumber'];

echo [0][0]['MLSNumber'];

The above list is some of the attempts i have used to grab the values but negative results.
 
Link to comment
Share on other sites

echo [0]['MLSNumber'];
echo [0][0]['MLSNumber'];
echo [0][0]['MLSNumber'];
There is no way those will work because they don't even mention $myarray. The variable has to be in there. Obviously.

 

echo $myarray['MLSNumber'];
echo $myarray[0]['MLSNumber'];
Better, but you're missing a very important part that can be seen quite clearly in the print_r output:

[DATA] => Array
(

Array
(
    [DATA] => Array
        (
            [0] => Array
                (
                    [CurrentPrice] => 135000.00
                    [MLSNumber] => 857252
Look at the whole structure and read from left to right through the indentation:

 

$myarray is

Array
(
an array containing

    [DATA] => Array
        (
a "DATA" element, which is also an array containing

            [0] => Array
                (
a 0 element, alongside a 1 and 2, which is also an array containing

                    [MLSNumber] => 857252
an "MLSNumber".

 

Put them all together in order and you get

$myarray["DATA"][0]["MLSNumber"]
Since the second index is a number 0-2 that means you'll need a loop to get all the elements. Like a foreach.

foreach ($myarray["DATA"] as $data) { // $data substitutes for the [0] element
	echo $data["MLSNumber"];
Link to comment
Share on other sites

Thank you Maddening..
Now this is a prime example of what happens when you teach a man to fish instead of giving him fish.. I took what he provided and finished my own solutions since i did not want to complicate it too much or even ask for help doing my entire project... but the final result was to get all the variables and even have the keys available if needed...so here is what i added totally due to this very exquisite tutorial.. i finished the project... Maddening could not finish it since he had no idea
what my final goal was but he did exactly what I asked for... This forum is the best.. not like "stackoverflow" who nitpick everything and if you forget to dot an i or cross a t they ban you.. I will never use them again..For each T that you do not cross you are banned for 3 days...Thankyou phpfreaks and especially Maddening you are the best... So bottom line i took what he gave me and learned from it and did not have to come back with but what if this or what if that.... i figured it out from his expert shove down the road..

foreach ($arrResult["DATA"] as $data) { // $data substitutes for the [0] element

	foreach ($data AS $key => $value) {
	echo "Value = $value<br>";
	}

	echo " &&&&& Separator &&&&&<br>";

}

Below is the output of my code mod which pulls out every entry for storage...

Value =
Value = 2088
Value =
Value =
Value =
Value = 135000.00
Value = 3412
Value = 857252
Value = 8971579
Value = 1
Value = 89005
Value =
Value = DENVER
Value = 1409
Value = Street
Value =
&&&&& Separator &&&&&
Value =
Value = 2088
Value =
Value =
Value =
Value = 135000.00
Value = 3412
Value = 857251
Value = 8971581
Value = 1
Value = 89005
Value =
Value = DENVER
Value = 1407
Value = Street
Value =
&&&&& Separator &&&&&
Link to comment
Share on other sites

Hey folks again i have to say you need to cut this post out and add it to your "How to do array notebook as it will be very valuable to you in future resolutions.

But I just noticed a possible confusion... some of you may be saying what the heck.. How did he get those values out of that example.. Sorry... i just simplified my version for the forum so here is the actual array i am running.. just so it does not look like i am crazy.. I should have made this match my original example but you can see the reason for my strange values compared to my original question.

[0] => Array                (                    [AdditionalPetRentYN] =>                     [AnnualPropertyTaxes] => 2088                    [BathsFull] =>                     [BathsHalf] =>                     [BathsTotal] =>                     [CurrentPrice] => 135000.00                    [DOM] => 3412                    [MLSNumber] => 857252                    [Matrix_Unique_ID] => 8971579                    [PhotoCount] => 1                    [PostalCode] => 89005                    [StreetDirPrefix] =>                     [StreetName] => DENVER                    [StreetNumber] => 1409                    [StreetSuffix] => Street                    [YearBuilt] =>                 )
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.