0

I'm working with XML data. when i try to specify an item from the array $item i got this error

[
  0 => array:4 [
    "Supplier_ID" => "34534565"
    "Supplier_Name" => "milan"
  ]
 1 => array:4 [
    "Supplier_ID" => "34534568"
    "Supplier_Name" => "adam"
  ]

and so on
$supplier_list_result = $supplier_list->body();
        $xml = simplexml_load_string($supplier_list_result);
        $json = json_encode($xml);
        $arrays = json_decode($json, TRUE);
        $supplier_name = [];
        foreach ($arrays as  $item) {
            $supplier_name = $item['Supplier_Name']; //when i use $supplier_name = $item it shows me the result above
        }
        dd($supplier_name);

miken32
  • 42,008
  • 16
  • 111
  • 154
Gene_Nostrada
  • 114
  • 1
  • 6
  • 2
    Try adding an inner loop `foreach ($item as $subitem) { $supplier_name = $subitem['Supplier_Name']; }`. As it is now `$supplier_name = $item[0]['Supplier_Name'];` should return the first element. – LMC Apr 21 '22 at 14:11

1 Answers1

1

Laravel has great helper methods such as data_get. You should be able to get all the Supplier_Name values with:

$supplier_names = data_get($arrays, '*.Supplier_Name);
John Zwarthoed
  • 1,239
  • 7
  • 12