2

I have tried to solve it, but I am lost. Can you see my mistake?

I have something wrong with:

array_push($change_data[$y],$_POST['new']['location'][$y]);

I get error:

array_push() expects parameter 1 to be array, integer given in...

‍$‍‍y is an integer but I need save it there. I need to create array:

$change_data(id => location = value)

$location_count = count($_POST['new']['id']);           
$change_data=array();  

for($y=0; $y<$location_count;$y++){ 
    if (!empty($_POST['new']['location'])) {
        array_push($change_data,$y);

        if (!empty($_POST['new']['location'][$y])) { 
            array_push($change_data[$y],$_POST['new']['location'][$y]);  
        }
    }
}
8
  • $change_data[$y] is an integer not an array, if you can specify more precise how you want $change_data to look then i can give you an better answer, you current example is unclear Commented Aug 25, 2016 at 15:10
  • so I just need to write $y=array(); right? Commented Aug 25, 2016 at 15:12
  • i think you want array_push($change_data,$_POST['new']['location'][$y]); , this will get a new index into the array $change_data with this value ($_POST['new']['location'][$y]) Commented Aug 25, 2016 at 15:14
  • 1
    $change_data[$y]=$_POST['new']['location'][$y] ?? Commented Aug 25, 2016 at 15:15
  • Please let us know what are you trying to do? forget about $y and array and just tell us what is desired? Commented Aug 25, 2016 at 15:17

2 Answers 2

4

The problem is you're dereferencing the value and then trying to use it as an array. That won't work. Because $change_data[$y] gives us a scalar integer value, but array_push expects an array value, not a scalar value (hence the error you're getting).

An easier way to do this is to just assign to the array directly. $change_data[] = $y is semantically equivalent to saying array_push($change_data, $y).

The other issue is $change_data[$y] may not always be what you think it is in this loop, because you're pushing to the array conditionally and $y increments linearly. You can't chose the key with array_push, but you can say $change_data[$y] = $y.

Finally your loop loops awkward because it makes some assumptions about PHP Arrays that aren't true. Namely that an array key is always a numeric value, sequentially incremented from 0.

Instead, if we actually evaluate what you're trying to do here more carefully you'll find that you're just trying to use $_POST['new']['id'] and $_POST['new']['location'] as an adjacency list. The more sane way to traverse PHP arrays and traversable objects is to use the foreach construct, which makes certain guarantees that your for loop here, simply does not.

Here's a simpler way to write the same code, but less prone to error.

$change_data = [];
foreach(array_filter($_POST['new']['id']) as $key => $value) {
    if (!empty($_POST['new']['location'][$key])) {
        $change_data[$key] = $_POST['new']['location'][$key];
    }
}

The difference here is that the foreach construct guarantees that we iterate over every element in array_filter($_POST['new']['id']) and array_filter() guarantees that no element in that loop will be considered empty(). Since the key is tracked by foreach and stored in the $key variable we make the same adjacency list assumptions and assign directly to $change_data under the same key value.

Sign up to request clarification or add additional context in comments.

Comments

1

PHP - array_push

When you use array_push, it place the new element at the end of the array, you can't decide the array key.

Thus array_push($change_data[$y],$_POST['new']['location'][$y]);
should be array_push($change_data,$_POST['new']['location'][$y]);

If you want to decide of the key, you will do this instead:
$change_data[$y]=$_POST['new']['location'][$y]);

1 Comment

Thanks very much :)

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.