0

I'm new to PHP and am not sure how to proceed. The array that I get back from decoding the JSOn is: (sorry if its formatted weird)

array(3) {
 [0]=> array(4) { 
   ["Name"]=> string(22) "Brent's Medical Center"
   ["date"]=> string(26) "/Date(1330449077600-0700)/"
   ["dealType"]=> string(13) "Capital Lease" 
   ["id"]=> string(11) "MO-N007175A" 
 } 
 [1]=> array(4) { 
   ["Name"]=> string(22) "Brent's Medical Center" 
   ["date"]=> string(26) "/Date(1330448929213-0700)/" 
   ["dealType"]=> string(2) "NA"  ..... ["id"]=> string(11) "MO-N007172Q" } [2]=> array(4) { ["Name"]=> string(15) "MOC" ["date"]=> string(28) "/Date(-62135571600000-0700)/" ["dealType"]=> string(2) "NA" ["id"]=> string(9) "MC" } }

I have used this foreach loop, but am not sure how to get each individual item out of an associative array.

foreach ($obj as $key => $value) {
    print_r($key);
}

This returns:

012

I have tried other solutions, but to no avail. Maybe I'm not understanding completely what's happening, but I can't get anything to do what I need/want. Thanks!

1
  • To clarify what I'm doing - I need to print out the data in a table format. So each array will essentially be it's own row, and each individual piece will be its own cell. That's what I'm trying to accomplish. Commented Mar 8, 2012 at 0:03

6 Answers 6

1

Well, it depends exactly how you want it returned.

foreach ($obj as $key => $value) {
   print_r($value);
}

Would return your data like this:

array(4) { 
 ["Name"]=> string(22) "Brent's Medical Center"
 ["date"]=> string(26) "/Date(1330449077600-0700)/"
 ["dealType"]=> string(13) "Capital Lease" 
 ["id"]=> string(11) "MO-N007175A" 
} 
array(4) { 
 ["Name"]=> string(22) "Brent's Medical Center" 
 ["date"]=> string(26) "/Date(1330448929213-0700)/" 
 ["dealType"]=> string(2) "NA"  
 ["id"]=> string(11) "MO-N007172Q" 
} 

... etc

If you wanted individual data pieces via your example, it would be like this:

foreach ($obj as $each_array) {
   foreach ($each_array as $val){
      echo $val . "<br>";
   }
}

Which would return:

Brent's MedicalCenter
/Date(1330449077600-0700)/
Capital Lease
... etc
Sign up to request clarification or add additional context in comments.

1 Comment

This is really close - To clarify what I'm doing - I need to print out the data in a table format. So each array will essentially be it's own row, and each individual piece will be its own cell. That's what I'm trying to accomplish.
0

You have nested objects, try the following:

echo '<table>';

foreach ($obj as $key => $value) {
    echo '<tr>';
    echo '<td>' . $value->Name . '</td>';
    echo '<td>' . $value->date . '</td>';
    echo '<td>' . $value->dealType . '</td>';
    echo '<td>' . $value->id . '</td>';
    echo '</tr>';
}

echo '</table>';

8 Comments

when I echo the $value["Name"] or whatever else, it echo's nothing and the rest of my script stops.
Can you add <?php ini_set('display_errors', 1); ?> to the top of your script and see if that reports a descriptive error?
It doesn't output anything. =(
Can you put the result of <?php print_r($obj); ?> in your question?
Fatal error: Cannot use object of type stdClass as array in /www/medo****/***.php on line 85
|
0

Assuming that $data is what was var_dump'd in your pasted content:

foreach($data as $record) {
    //$record['name'] is now something like "Brent's medical center"
}

Note though that you'll have to process the date field into something more usable than a string.

Comments

0

Script echo's exactly what you ask it to echo - array keys(indexes). Array has 3 values, so its keys are 0, 1, 2.

Looks like you need $value variable insode foreach-loop.

I hope it helps.

Comments

0

Here's a simplified version of your problem. Substitute your array for the one here.

<?php
$arr[0] = array('uno'=>'one', 'dos'=>'two');
$arr[1] = array('AAAA'=>'aaaa', 'BBBB'=>'bbbb');

foreach ($arr as $obj) {
    foreach ($obj as $k=>$v) {
        echo "key:$k=>val:$v\n";
    }
}
?>

If you want to access something specific, you can do it like this:

echo $arr[1]["BBBB"]; // echoes bbbb

Or...

echo $arr[1]["Name"]; // echoes Brent's Medical Center

Comments

0

Your variable is an array filled with associative arrays. So when you're doing your loop and operating on $key, that's not the data, but the index of your parent array. So simply changing the part of the array you're dealing with will fulfill your original code sample.

foreach ($obj as $key => $value) {
    print_r($value);
}

Now each $value is the associative array with the keys Name, date, dealType, etc. So you can get your values directly, e.g. $value['Name'] for the first loop would be "Brent's Medical Center"

7 Comments

Thats closer - now how can I get the information out of the value array? Sorry.. I'm a total newb.
Exactly as I mentioned after the code sample: $value['Name'] (modified further to attempt to be more clear). Is there something else you're trying to do besides that?
When I try and echo the $value['Name'] it returns the entire $obj array, and the rest of my script stops.
Actually thats not whats happening - sorry. I still cant access individual pieces with echo $value['Name'];
To clarify what I'm doing - I need to print out the data in a table format. So each array will essentially be it's own row, and each individual piece will be its own cell. That's what I'm trying to accomplish.
|

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.