#
#
# artur #2016-01-30 17:47:13
PHP
# artur #, 2016-01-30 17:47:13

How to change the names of keys in an array?

Hello dear!
Today I sat down (after half a year) for an unfinished project and saw that one function was working with an error.
Help, please solve the problem.
I have an array:

Array ( [0] => Array ( [toy_id] => 222 [count] => 2 ) [1] => Array ( [toy_id] => 333 [count] => 1 ) )

There is a task to get the following ( or something similar , for convenient use - displaying the number of toys by ID):
Array ( [222] => 2, [333] => 1)
I rummaged through the list of standard PHP functions, I did not find a simple and quick solution.
I need your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dimabdc, 2016-01-30
@passshift

$newArray = array_column($array, 'count', 'toy_id');

P
Peter, 2016-01-30
@petermzg

Probably so

$newarray = array();
foreach ( $array as $key => $value ) {
  $newarray = $newarray + array($value["toy_id"] => $value["count"]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question