3

I am having difficulty to write all of the array of objects into .json file. Here is my code. Using this code, I am just getting last object of array in .json file but I have 6 objects in total and successfully array is printing in terminal. Can anyone help me please? Thanks

foreach($crawler as $node) {
    
        $title = $node->filter('h3')->text();
        $img = $node->filter('img')->attr('src');
        $color = $node->filter('div.my-4 div.flex div.px-2 span')->attr('data-colour');
        $capacity = $node->filter('span.product-capacity')->text();
        $availibity = $node->filter('div.text-sm')->text();
        $shippingText = $node->filter('div.bg-white > div')->last()->text();
        $shippingDate = $node->filter('div.bg-white > div')->last()->text();
        $productArray = array(
    
          'title' => $title,
          'price' => 12,
          'imageUrl'=> 'https://www.magpiehq.com/developer-challenge/smartphones/'.$img,
          'capacityMB' => $capacity,
          'colour' => $color,
          'availabilityText' => $availibity,
          'shippingText' =>$shippingText,
          'shippingDate' =>$shippingDate
        );
        
        $json = json_encode($productArray);
        file_put_contents("output.json", $json);
    
      }
6
  • Your call to file_put_contents() is inside your loop, so each iteration overwrites the previous. Build an array of arrays inside the loop, then put your file_put_contents() after the loop closes. Commented Jul 5, 2022 at 21:36
  • @AlexHowansky I just did and it still displaying last object instead of six into json file Commented Jul 5, 2022 at 21:39
  • @ku234 perhaps you didn't quite get it right then. See my example in the answer below. Commented Jul 5, 2022 at 21:40
  • Can you please show me some hint about build array of array inside? Commented Jul 5, 2022 at 21:40
  • @ADyson Sure, I am looking forward to it. Thanks Commented Jul 5, 2022 at 21:41

1 Answer 1

1

Because you're writing to the file inside the loop, you keep overwriting its contents every time.

To write all the data to a file, and make it write a valid JSON entity which will be decodable again later, you need to construct a single array containing all your product data, and then encode and write that array to the file once, after the loop has ended.

For example:

$products = array();

foreach($crawler as $node)
{
    $title = $node->filter('h3')->text();
    $img = $node->filter('img')->attr('src');
    $color = $node->filter('div.my-4 div.flex div.px-2 span')->attr('data-colour');
    $capacity = $node->filter('span.product-capacity')->text();
    $availibity = $node->filter('div.text-sm')->text();
    $shippingText = $node->filter('div.bg-white > div')->last()->text();
    $shippingDate = $node->filter('div.bg-white > div')->last()->text();
    
    $productArray = array (
          'title' => $title,
          'price' => 12,
          'imageUrl'=> 'https://www.magpiehq.com/developer-challenge/smartphones/'.$img,
          'capacityMB' => $capacity,
          'colour' => $color,
          'availabilityText' => $availibity,
          'shippingText' =>$shippingText,
          'shippingDate' =>$shippingDate
    );
    
    $products[] = $productArray; //add the current item to the overall array
}

//encode all the data at once, and then write it to the file
$json = json_encode($products);
file_put_contents("output.json", $json);
Sign up to request clarification or add additional context in comments.

1 Comment

Great. Do you understand why?

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.