php范围从已经给定的名称变量数组

时间:2017-06-21 06:01:56

标签: php arrays laravel web foreach

所以我在变量$ Myarray

中有这个数组
{ [0]=> object(stdClass)#3216 (3) 
{ ["id"]=> string(2) "11" ["name"]=> string(5) "david"}
{ ["id"]=> string(2) "12" ["name"]=> string(5) "linda"}
{ ["id"]=> string(2) "13" ["name"]=> string(5) "dony"}

所以我想使用foreach范围回显id 12到13,问题是..我该怎么做?

因为在php中,我只能搜索特定的数组,如:

<?php
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach (range(0, 12) as $number) {
    echo $number;
}

// The step parameter
// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
foreach (range(0, 100, 10) as $number) {
    echo $number;
}

// Usage of character sequences
// array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
foreach (range('a', 'i') as $letter) {
    echo $letter;
}
// array('c', 'b', 'a');
foreach (range('c', 'a') as $letter) {
    echo $letter;
}
?>

感谢

1 个答案:

答案 0 :(得分:1)

“$ object”包含您发布的上述数据。

$data = $object[0];
foreach ($data as $key => $val) {
  if($val->id == 12 || $val->id == 13)
    echo $val->name;
}