0

I have an empty array ($report), and i want to create a multidimensional array with element of "$array" The problem is that adds elements only for index = 2 (last interation). Why?

<?php
 $array= array("3,4","5,6","7,8");
 $report= array();
 for($i=0, $n= count($array); $i< $n; $i++){
      $lat= substr($array[$i],0,1);
      $lng= substr($array[$i],2,1);  
      $report= array(array($lat,$lng));
  }
  echo "<pre>";  print_r($report);

   ?>
3
  • You rewrite the array, to add item use $report[]= array(array($lat,$lng)); Commented Aug 1, 2015 at 18:10
  • yeah so the problem it was only the method to add elements at array! If you write an answer i check it :) thanks a lot! Commented Aug 1, 2015 at 18:14
  • glad to help. I've written the answer. Good luck! Commented Aug 1, 2015 at 18:18

2 Answers 2

1

In the line $report= array(array($lat,$lng)); you reinit array every pass of the loop. To add new item to array rewrite it to

$report[]= array(array($lat,$lng));
Sign up to request clarification or add additional context in comments.

Comments

0

You are replacing the value of $report every iteration. Use this syntax to append to the array:

$report[] = array(array($lat,$lng));

The [] appends values to the end of an array.

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.