简体   繁体   中英

PHP Combine fields in multidimentional array

Forgive my explanation of this as I am not a developer.

I have a query from php that produces the last five results from a teams matches. I would like to display the outcome of those matches within another array.

I have the following

$_teams_form = $this->getFormResults($t['team']);

$team['form'] = $_teams_form[0]['form_result'].' '.$_teams_form[1]['form_result'].' '.$_teams_form[2]['form_result'].' '.$_teams_form[3]['form_result'].' '.$_teams_form[4]['form_result'];

This works and I can use the $team['form'] to display my data. But I know the number of the arrays as I am limiting the records to 5 but if I change that then the code breaks. I also want to change what I display depending on the contents of the $_teams_form[0]['form_result'] fields.

There must be a better way to manipulate this but my mind has gone blank, appreciate any help.

Since you can't use plain implode on that array, you're just gonna have to do the loop yourself:

$_teams_form = $this->getFormResults($t['team']);
$len = count($_teams_form);
$team['form'] = '';
if($len > 0) // Make sure $_teams_form[0] exists
{
    $team['form'] .= $_teams_form[0]['form_result'];
    for($i = 1; $i < $len; $i++) // $i = 1 because we've got 0 already
    {
        $team['form'] .= ' '.$_teams_form[$i]['form_result'];
    }
}

However, the real question (to me) is: Why is a non-developer required to do this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM