嵌套foreach中的两个对象数组

I have two variables:

  • $foo - client1, client2, client3 (an object array)
  • $bar - name, id, turnover (a string array)

If I echo $foo->name of client 1 it returns successful value, but:

foreach ($foo as $key1 => $value1) {

  foreach ($bar as $key2 => $value2) {

    echo $value2->$value1; // THIS IS NOT WORKING

  }

}

Hope I'm clear; I want to return these values:

client1's name
client1's id
client1's turnover
client2's name 
client2's id
etc...

This is successful:

foreach($foo as $client) {

  echo $client->name."<br>";
  echo $client->id."<br>";
  echo $client->billable."<br>";

}

returns client name, his id and if he's billable or not for every client. But the above code is not working. Name, id and billable are stored in a string thus:

$bar = array ([0] => name, [1] => id, [2] => billable )

You need to replace

$value2->$value1

With

$value1->$value2

If i understand you correctly example

$bar = array("name", "id", "turnover");
$foo = array(
        (object) array_combine($bar,range(1,3)),    //client 1
        (object) array_combine($bar,range("A","C")),    //client 2
        (object) array_combine($bar,range("X","Z")),    //client 3
);



foreach ($foo as $key1 => $value1) {
    foreach ($bar as $key2 => $value2) {
        echo "Clients $key1 $value2 = ",$value1->$value2 ,PHP_EOL; // THIS IS NOT WORKING
    }
    echo PHP_EOL ;
}

Output

Clients 0 name = 1
Clients 0 id = 2
Clients 0 turnover = 3

Clients 1 name = A
Clients 1 id = B
Clients 1 turnover = C

Clients 2 name = X
Clients 2 id = Y
Clients 2 turnover = Z

Giving meaningful names to variables, and using curly braces access:

foreach ($clients as $i => $client) {

  foreach ($keys as $j => $key) {

    echo $client->{$key};

  }

}

This is working:

$foo = array( "client1", "client2", "client3"); 
$bar = array("name","id", "turnover");

foreach ($foo as  $value1) {
    foreach ($bar as $value2) {
        echo $value1 . "->" . $value2 . "
";
    }
}

http://sandbox.onlinephpfunctions.com/code/ebdba5ef40498d4ead4be9a281f715565717471a

I don't know where was your problem, I guess some typo.You didn't write what was your output and your code is half pseudo code (for example to concatenate the two values you don't use ->). You should provide more details and more accurate code.