2

Im wondering if array_column keeps the array elements order so when i call the function twice in same array, i can trust the resulting arrays will match. I need to work this way because in the code I have to make many operations with only specific data (one of the keys, in the example, the id), and after all, use both of them (in example id and qty) to insert in db.

Example:

QUERY: SELECT id, qty FROM items ...
CONVERTED INTO ASSOCIATIVE ARRAY:
$array = [
  ['id' => 1, 'qty' = 2],
  ['id' => 2, 'qty' = 4]
];

$itemsIds = array_column($array, 'id');
$itemsQty = array_column($array, 'qty'); 

Can i do?:

for($i = 0; $i < count($array); $i++) {
  echo $itemsIds[$i] . $itemsQty[$i];
}

And i will always get the correct data for each item?

4
  • not really sure what you are attempting to acomplish with your above code, if you can explain what you are trying to do it might help. In short the answer is no because you are using array_unique which is stripping duplicate values, which means your two arrays could be two different lengths. Commented Jul 21, 2016 at 19:17
  • Why don't you get it just with the query? Commented Jul 21, 2016 at 19:17
  • 1
    array_unique() is an issue because after that arrays may be different lengths. And there doesn't seem to be any reason for doing what you're doing. Can you explain? Commented Jul 21, 2016 at 19:46
  • Yeah sure i didn't realize in the code I included I also wrote the array_unique function which obviously could be a game changer. And no, I can't use both keys on the same loop because the flow of the program. Commented Jul 24, 2016 at 8:27

3 Answers 3

2

From your code it seems like you wanna keep keys corresponding to its value correctly after you use array_column twice.

But for you information you can get this by using array_column just once like this:

$items = array_column($array, 'qty', 'id')

Now you get an associative array whose keys are each row’s id and values are each row’s qty.

The third parameter's meaning of array_column is

The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name.

You can find more explains and examples from documentation: http://php.net/manual/en/function.array-column.php

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

Comments

1

Since the DB result is row-based (usually), every column will have some value (even if it's NULL). So unless you mess with the result somehow, you should be safe. I do practically the same in some of my code.

Comments

0

What if you get all values which you need through SQL query and print it with something like:

$arrayLength = count($array); // Better optimization than do it in every loop
for($i = 0; $i < $arrayLength; $i++) {
  echo $array[$i]['id'] . $array[$i]['qty'];
}

That will make you sure that all match with all, and that they have the same quantity of rows.

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.