1

First I have this associative array

$players = array("group1" => array("player1" => "Kevin Durant", "player2" => "Kyrie Irving"), "group2" => array("player1" => "Demian Lillard", "player2" => "Derrick Rose"));

I know I can just use this way of looping

//create an empty array to be used as a container for our looped array
$data_arrays = array();
foreach($players as $data=>$key):
    array_push($data_arrays,$key['player1'],$key['player2']);
endforeach;

but what I want is get there value base on there array index, e.g.

foreach($players as $data=>$key):
    array_push($data_arrays,$key[0],$key[1]);
endforeach;

but unfortunately and sadly, it returms me an error,

Undefined offset: 0

any help, clues, suggestions, recommendations, ideas please?

2 Answers 2

2

You could use array_values():

foreach($players as $data=>$key):
    $groupPlayers = array_values( $key );
    array_push($data_arrays,$groupPlayers[0],$groupPlayers[1]);
endforeach;

As a side note: your use of the variable names $data and $key is rather unorthodox: the key is the index of an array element, data is usually considered the value of the array element. So I would suggest to switch them, i.e.:

foreach($players as $key=>$data):
    $groupPlayers = array_values( $data );
    array_push($data_arrays,$groupPlayers[0],$groupPlayers[1]);
endforeach;
Sign up to request clarification or add additional context in comments.

5 Comments

cool, how about if I'm going to get the name of the array object, e.g. I want to get the name of $players[0] which is 'player1', any ideas how to do that please?
@CodeDemon For that you could simply use the value of the $key variable that's already defined (in my second example, that is). Else you could use array_keys(), in the same way I demonstrated the use of array_values().
As a side note: If you have numeric indices, array_values() will ignore them and index your array based on the foreach() ordering.
@CodeDemon Sorry, the first statement in my reply comment was not correct, of course. $key will hold "group1", etc. (the key value of the outer array), not "player1", etc.
@CodeDemon Another sloppy thing of me is that you should choose another variable name than $players inside the foreach loop; otherwise it will overwrite the original $players array. Sorry about that; I'll correct this in my answer.
2

Something like this would work:

<?php
$players = array("group1" => array("player1" => "Kevin Durant", "player2" => "Kyrie Irving"), "group2" => array("player1" => "Demian Lillard", "player2" => "Derrick Rose"));

$data_arrays = array();
foreach($players as $data => $key):
    $data_arrays = array_merge($data_arrays, array_values($key));
endforeach;

print_r($data_arrays);

Demo: http://sandbox.onlinephpfunctions.com/code/e5133c3f317beccc5c2b0bbd56770359a1040c37

An Alternative, shorter way to do it using array_reduce:

$players = array("group1" => array("player1" => "Kevin Durant", "player2" => "Kyrie Irving"), "group2" => array("player1" => "Demian Lillard", "player2" => "Derrick Rose"));

$players = array_reduce($players, function(&$players, $v) {return array_merge(array_values($players), array_values($v));}, array());

print_r($players);

Demo: http://sandbox.onlinephpfunctions.com/code/92c51ac92fdfde40df3e4fc9d469d52d19f05376

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.