Home > Back-end >  How to get required data from this array
How to get required data from this array

Time:10-19

I am using Laravel. I have a complex array and i want to make it more readable and I am not sure how to convert it into more readable form and i want to pass only the required information. this is loop

foreach($dataArray as  $value){
           $addressArray[] = [
            'id' => $value['place_id'],
       
            'address'=> $value['terms'],

           ];
        }
        dd($addressArray);
    }

and this array returns response in following format and I want to format this array

   array:5 [
  0 => array:4 [
    "id" => "ChIJH6WK3tQ0K4gRm0XKDAjRGfA"
    "label" => "12 York Street, Toronto, ON, Canada"
    "value" => "12 York Street, Toronto, ON, Canada"
    "address" => array:5 [
      0 => array:2 [
        "offset" => 0
        "value" => "12"
      ]
      1 => array:2 [
        "offset" => 3
        "value" => "York Street"
      ]
      2 => array:2 [
        "offset" => 16
        "value" => "Toronto"
      ]
      3 => array:2 [
        "offset" => 25
        "value" => "ON"
      ]
      4 => array:2 [
        "offset" => 29
        "value" => "Canada"
      ]
    ]
  ]
  ]
  2 => array:4 [
    "id" => "ChIJJa4u6nqipBIRthG2uMmbv2M"
    "label" => "Avinguda de Rius i Taulet, 12, Barcelona, Spain"
    "value" => "Avinguda de Rius i Taulet, 12, Barcelona, Spain"
    "address" => array:4 [
      0 => array:2 [
        "offset" => 0
        "value" => "Avinguda de Rius i Taulet"
      ]
      1 => array:2 [
        "offset" => 27
        "value" => "12"
      ]
      2 => array:2 [
        "offset" => 31
        "value" => "Barcelona"
      ]
      3 => array:2 [
        "offset" => 42
        "value" => "Spain"
      ]
    ]
  ]
  3 => array:4 [
    "id" => "ChIJUXFxe6qMwokRT2J7mbHQE3o"
    "label" => "1250 Waters Place, Bronx, NY, USA"
    "value" => "1250 Waters Place, Bronx, NY, USA"
    "address" => array:5 [
      0 => array:2 [
        "offset" => 0
        "value" => "1250"
      ]
      1 => array:2 [
        "offset" => 5
        "value" => "Waters Place"
      ]
      2 => array:2 [
        "offset" => 19
        "value" => "Bronx"
      ]
      3 => array:2 [
        "offset" => 26
        "value" => "NY"
      ]
      4 => array:2 [
        "offset" => 30
        "value" => "USA"
      ]
    ]
  ]
  4 => array:4 [
    "id" => "ChIJfz6STcJYwokRFONBxt9Ytvs"
    "label" => "1275 York Avenue, New York, NY, USA"
    "value" => "1275 York Avenue, New York, NY, USA"
    "address" => array:5 [
      0 => array:2 [
        "offset" => 0
        "value" => "1275"
      ]
      1 => array:2 [
        "offset" => 5
        "value" => "York Avenue"
      ]
      2 => array:2 [
        "offset" => 18
        "value" => "New York"
      ]
      3 => array:2 [
        "offset" => 28
        "value" => "NY"
      ]
      4 => array:2 [
        "offset" => 32
        "value" => "USA"
      ]
    ]
  ]
]

However I want to format data in this format.

array:5 [
  0 => array:4 [
    "id" => "ChIJH6WK3tQ0K4gRm0XKDAjRGfA"
    "label" => "12 York Street, Toronto, ON, Canada"
    "value" => "12 York Street, Toronto, ON, Canada"
    "address" => [
        'number' => 12,//from address array, index 0 value
        'street'=>'York Street',//from address array, index 1 value
        'city'=>'Tornonto,//from address array, index 2 value
        'state=>'ON',//from address array, index 3 value
        'country'=>'Canada'//from address array, index 4 value
    ];

    }
    

Any help? Thanks

CodePudding user response:

You can set the value by referencing each index of the terms key manually

$number = '';
$street = '';
$city = '';
$state = '';
$country = '';

if (isset($value['terms'][0]['value']))
    $number = $value['terms'][0]['value'];

if (isset($value['terms'][1]['value']))
    $street = $value['terms'][1]['value'];

if (isset($value['terms'][2]['value']))
    $city = $value['terms'][2]['value'];

if (isset($value['terms'][3]['value']))
    $state = $value['terms'][3]['value'];

if (isset($value['terms'][4]['value']))
    $country = $value['terms'][4]['value'];

$addressArray[] = [
    'id' => $value['place_id'],
    'label' => $value['description'],
    'value' => $value['description'],
    'address' => [
        'number' => $number,
        'street' => $street,
        'city' => $city,
        'state' => $state,
        'country' => $country
    ]
];
  • Related