0

I'm trying to take out files out of an Array that defined like this:

$arr[$j][$player_number]['field_name'];

I have 2 fields in the array : names and rating. The output when I'm printing the array is :

Array ( [$j] => Array ( [names] => "some name" [rating] => "some number" ))

My problem is that I can't take the fields "names" and "rating" at the same loop when I'm doing "for each". When I'm doing it separately - it works but I can't sort it in a table normally. I know that I have to use the "list" in some way, I tried to execute it, but without success.

What I'm doing is :

function get_array_column($array, $column)
    {
        $ret = array();
        foreach ($array as $row) $ret[] = $row[$column];
        return $ret;
    }
    for($i=1;$i<=$num_groups;$i++)
    {   
        $a = array();
        $a = get_array_column($arr[$i],'names');
        foreach ($a as $value)
           {
             echo $value;
           }
    }

Thanks.

2
  • 2
    Please add the code that you have written so far, as it is difficult to understand what you're trying to do here. Commented Oct 16, 2014 at 12:05
  • @i-alarm-edalien The code was added. Commented Oct 16, 2014 at 12:16

2 Answers 2

1

once you defined your array as

[0] => Array ( [name] => name0 
[fieldname] => fieldname0 ) [1] => Array ( [name] => name1 [fieldname] => fieldname1 )

foreach ($arr as $key){

echo $key['names'];

echo $key['ratings'];

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

1 Comment

This is exactly the answer to my question.
0

You are having a three dimensional array.

  1. dimension indicated by $j
  2. dimension indicated by $player_number
  3. dimension indicated by $field_name

Maybe you want something like this: An array which is indicated by the number of the player $array[$player_number]

In this array you can add stdClass objects, which contain the name and the rating of the player. The code could look like this

//create players array
$players = array();
//create a player
$player = new stdClass();
$player->number = 342;
$player->name = "Matt Click";
$player->rating = 13;
// add a player to the players array
$players[$player->number] = $player;
... // some code
// foreach through the players
foreach ($players as $player)
{
    echo "Name: ".$player->name." rating:".$player->rating."\n";
}

2 Comments

Maybe it will work, but it's not what I'm searching. The array is ready and the only thing that I'm trying to do is to output 2 fields at the same loop. And the field_name is not a variable (without $), there are only 2 fields each array - names and rating.
Please tell me again how your array is looking like, cause $arr[$j][$player_number]['field_name']; means something else as you are saying.

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.