0
1. Extracted from my laravel controller:
 ..
 ..
 $data = json_decode($response, true);
 return $data;
 ..
 ..
 return view('homepage')->with('homeExclusives', $homeExclusives);
  1. Here is a sample of the returned data, just a short version, since the returned feed is very large, but this will give you an idea of the way it's structured.
 array(4) {
 ["success"]=> bool(true) 
 ["status"]=> int(200) 
 ["bundle"]=> array(2) {
 [0]=> array(631) {
 ["StreetDirPrefix"]=> string(2) "SW" 
 ["DistanceToStreetComments"]=> NULL
 }
 [1]=> array(631) { 
 ["StreetDirPrefix"]=> string(2) "NE" 
 ["DistanceToStreetComments"]=> NULL
 }
 }
  1. I need to extract "StreetDirPrefix" value from [0] and [1], but I always get an error. Can someone help?
asked Jul 17, 2019 at 20:12
1
  • Show what you've tried, please. What error are you getting? Commented Jul 17, 2019 at 20:16

2 Answers 2

1

For the data in your example you might use array_column and specify StreetDirPrefix as the column key.

$res = array_column($array["bundle"], "StreetDirPrefix");
print_r($res);

Php demo

answered Jul 17, 2019 at 20:22

3 Comments

Thank you, how can I use a loop to list all of the "StreetDirPrefix"?
Excellent example, one more question, how can I make it work if I want to extract multiple values and display them, I'm trying to create a featured properties row, example "StreetDirPrefix", "UnparsedAddress", "SalePrice" & "mainImage" and also not show the "0 => ".
That is a different question and a very broad one as well. That depends how the data is structured in the array. In that case my advice would be to ask a new question adding the code and specifying the data and the desired result.
0

Without knowing what error you are getting my solution would be something like this:

<?php
if (is_array($data) && is_array($data["bundle"]) ) {
 foreach ($data["bundle"] as $tmpKey => $tmpVal) {
 if (isset($tmpVal["StreetDirPrefix"])) {
 echo $tmpKey." => ".$tmpVal["StreetDirPrefix"]."\n";
 }
 }
}
?>

I always like to validate arrays, so if your $data variable or the $data["bundle"] subsection are not arrays then you will not get anything. Not even an error.

I have a working example here: https://www.seeque-secure.dk/demo.php?id=PHP+how+to+loop+over+nested+JSON+Object

EDIT:

(if i understand you correct) When you have validated your array all you have to do is repeat the inner validation like this:

<?php
if (is_array($data) && is_array($data["bundle"]) ) {
 foreach ($data["bundle"] as $tmpKey => $tmpVal) {
 if (isset($tmpVal["StreetDirPrefix"])) {
 echo $tmpKey." => ".$tmpVal["StreetDirPrefix"]."\n";
 }
 if (isset($tmpVal["UnparsedAddress"])) {
 echo $tmpVal["UnparsedAddress"]."\n";
 }
 if (isset($tmpVal["SalePrice"])) {
 echo $tmpVal["SalePrice"]."\n";
 }
 //...... ect.....
 }
}

?>

answered Jul 17, 2019 at 20:41

2 Comments

Excellent example, one more question, how can I make it work if I want to extract multiple values and display them, I'm trying to create a featured properties row, example "StreetDirPrefix", "UnparsedAddress", "SalePrice" & "mainImage" and also not show the "0 => ".
I actually analized the you example and was able to remove the => and loop over other values, thank you very much.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.