php 如何从laravel中的数组中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29531385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:27:29  来源:igfitidea点击:

how to get value from array in laravel

phplaravel-4

提问by Shojib Flamon

In laravel i get this type of array from where query. i want to get only those menu where parent is 0;

在 Laravel 中,我从 where 查询中获得了这种类型的数组。我只想获取父项为 0 的菜单;

 $memus  = Menu::where('parent', 0)->get()->toArray();

 Array
    (
        [0] => Array
            (
                [id] => 13
                [name] => Movies
                [parent] => 0
                [deleted_at] => 
                [created_at] => 2015-04-07 02:48:48
                [updated_at] => 2015-04-07 02:48:48
            )

        [1] => Array
            (
                [id] => 16
                [name] => zxcvxc
                [parent] => 0
                [deleted_at] => 
                [created_at] => 2015-04-07 02:53:26
                [updated_at] => 2015-04-07 03:03:39
            )

        [2] => Array
            (
                [id] => 17
                [name] => alsdkf
                [parent] => 0
                [deleted_at] => 
                [created_at] => 2015-04-07 02:53:41
                [updated_at] => 2015-04-07 03:02:04
            )

    )

So how to get particular value from this array i have tried echo $abc->nameand echo $abc->idbut not access

因此,如何从这个数组获取特定值我曾尝试echo $abc->nameecho $abc->id,但不能上网

回答by Laurence

You can just do this:

你可以这样做:

echo $memus[0]['name'];

Or if you want all of them

或者如果你想要所有这些

foreach ($memus as $memu) {
       echo $memu['name'];
}

回答by Joe

The arrow ($object->property)notation is for objects.

箭头($object->property)符号用于对象。

The notation for accessing array elements is $array[$index][$key].

访问数组元素的符号是$array[$index][$key]

So in your case, to access the name key in your second array would be:

因此,在您的情况下,要访问第二个数组中的 name 键将是:

echo($menu[1]['name']), in your example this would echo the string 'zxcvxc'.

echo($menu[1]['name']),在您的示例中,这将回显字符串“zxcvxc”。

回答by Kapil Verma

You can use collections in laravel 5

您可以在 Laravel 5 中使用集合

collect($arrayName);

$filtered = $arrayName->where('parent', 0);

$filtered->all();

Hope this will help. If not please let me know.

希望这会有所帮助。如果没有,请告诉我。

回答by Zahid

To get a particular value

获取特定值

{{memus[0]->id}}

回答by Brahms

If you want to access some array's data just do $array_name[index]->keyin the controller or in the view.

如果您想访问某个数组的数据,只需 $array_name[index]->key在控制器或视图中执行即可。

Index is usually an integer and the key is what you are looking to extract. For instance, we would do this on your array: $menu_zero = $menus[0]->id;it would give us 13 and $menu_name_zero = $menu[0]->name;would give the name.

索引通常是一个整数,键是您要提取的内容。例如,我们会在您的数组上执行此操作:$menu_zero = $menus[0]->id;它会给我们 13 并$menu_name_zero = $menu[0]->name;会给出名称。

回答by Niamul

You can do it this way:

你可以这样做:

For single item you can do this:

对于单个项目,您可以这样做:

echo ($menus[0]['parent'] == 0) ? $menus[0]['name'] : '';

With foreachloop:

foreach循环:

 foreach ($menus as $menu) {
        if($menu['parent'] == 0){
            echo $menu['name'];
        }
    }

回答by RahmanRezaee

Use this line for me worked

使用这条线为我工作

  $memus  = Menu::where('parent', 0)->get()->toArray();
    $arr = array();
    foreach ($memus as $s) {
        array_push($arr,$s->buy_product_id)
    }