1

In laravel i get this type of array from where query. i want to get only those menu where parent is 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->name and echo $abc->id but not access

1
  • Use foreach loop and you have to get it as array as $menu['name'] or if you want to use it as object then don't convert the result using toArray() then you can fetch it as $menu->name. Commented Dec 12, 2019 at 12:13

8 Answers 8

11

You can just do this:

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

Or if you want all of them

foreach ($memus as $memu) {
       echo $memu['name'];
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

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

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

Comments

1

You can use collections in laravel 5

collect($arrayName);

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

$filtered->all();

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

1 Comment

$collection->firstWhere('parent', 0) to get the item. doc
1

To get a particular value

{{memus[0]->id}}

Comments

1

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)
    }

Comments

0

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

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.

Comments

0

You can do it this way:

For single item you can do this:

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

With foreach loop:

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

Comments

0

Query :: $firstCategory = ProductCategory::select('id')->offset(0)->limit(1)->get()->toArray();

How to display value :: $firstCategory[0]['id'];

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.