1

I have a response in below format. I am using laravel. I want to check if there is street_number present or not despite of their index and if yes just print the long_name similar I want to check if there is route in types of this array or not. if yes print route long_name and so on.

dd($dataArray); gives this response

array:3 [
  "html_attributions" => []
  "result" => array:15 [
    "address_components" => array:7 [
      0 => array:3 [
        "long_name" => "19"
        "short_name" => "19"
        "types" => array:1 [
          0 => "street_number"
        ]
      ]
      1 => array:3 [
        "long_name" => "Becket Street South"
        "short_name" => "Becket St S"
        "types" => array:1 [
          0 => "route"
        ]
      ]
      2 => array:3 [
        "long_name" => "senroy"
        "short_name" => "Slenroy"
        "types" => array:2 [
          0 => "locality"
          1 => "political"
        ]
      ]
]

I am looping like this

$newArray=[];
foreach($dataArray['result'] as $data){
//    check if street_number in array or not if yes push it value to new array
//same for other types as well

}
3
  • just check inside types via in_array so that you don't need to use a hardcoded index, it'll just check if street_address exists Commented Oct 20, 2021 at 2:00
  • i did this if (in_array('street_number', $data[$key]['types'])){ // it gives error "Undefined index: types" } Commented Oct 20, 2021 at 2:04
  • but this is fine if (in_array('street_number', $data[0]['types'])). but i don't want to hardcode Commented Oct 20, 2021 at 2:05

2 Answers 2

2

the inside of your foreach is problematic: should be like this:

foreach($dataArray as $key=> $data){
    if (in_array('street_number',$data['types'])) {
        
    }
}

because your types is already in $data inside the foreach. So you don't need to say $key again.

Edit 1: found the problem. remove isset($data[$key]) because it is always false. because the $key is returning 0,1,.... but $data keys are these three : 'long_name' , 'short_name' , 'types' . You can check by dd($key)

Edit 2: After your latest update of the question, your foreach should be like this:

foreach($dataArray['result']['address_components'] as $data){
    if (in_array('street_number',$data['types'])) {
        dd('should be here');
    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

not working: if (isset($data[$key]) && in_array('street_number',$data['types'])) { dd('yes'); }I have not got the yes answer which means it was skipped
problem found and updated in the answer @David
message: "Undefined index: types" got error
Share var_nump($data) from the loop
Honestly as long you do dd($variableGivingWarning) you should be able to figure out which array keys to use. Your top-level loop should start with $dataArray['result']['address_components'] instead of $dataArray['result']
|
0

May this can help you

foreach ($data['result']['address_components'] as $val) {
    if(\Illuminate\Support\Arr::has($val, 'types') && in_array('street_number', $val['types'])) {
        echo "For Street Nu.";
    }else if(\Illuminate\Support\Arr::has($val, 'types') && in_array('route', $val['types'])) {
        echo "For Route";
    }
}

Comments

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.