0

I just can't get this array to play nice. I'm using Laravel 4.2 and am throwing: ErrorException (E_UNKNOWN) Undefined offset: 1

$tags = Test\Zero::get_tags();

dd ($tags);

    array (size=2)
  0 => 
    array (size=4)
      'guid' => string '-a035-4742-bb65-c44' (length=36)
      'name' => string 'tagtest1' (length=9)
      'updateSequenceNum' => int 14
      'parentGuid' => null
  1 => 
    array (size=4)
      'guid' => string '-905e-466e-9e91-33ddggg4' (length=36)
      'name' => string 'testtag2' (length=11)
      'updateSequenceNum' => int 19
      'parentGuid' => null

$tags = array_dot(`$tags);


array (size=8)
  '0.guid' => string '-a035-4742-bb65-c44' (length=36)
  '0.name' => string 'tagtest1' (length=9)
  '0.updateSequenceNum' => int 14
  '0.parentGuid' => null
  '1.guid' => string '-905e-466e-9e91-33ddggg4' (length=36)
  '1.name' => string 'testtag2' (length=11)
  '1.updateSequenceNum' => int 19
  '1.parentGuid' => null


$name = array_fetch($tags, '1.name');
echo $name;

How can I loop through to display all values with key 'name' ? Everything I try gives ErrorException (E_UNKNOWN) Undefined offset:

Any help would be greatly appreciated.

2
  • 1
    A simple loop like foreach ($tags as $tag) { echo $tag['name']; } will do - if you have PHP5.5, you can also use $names = array_column($tags, 'name'); to retrieve an array of all the names us1.php.net/manual/en/function.array-column.php Is that what you are looking for? Commented Sep 16, 2014 at 1:24
  • How bizarre - thought had tried that - it works - Thankyou!! Commented Sep 16, 2014 at 1:30

1 Answer 1

1

While @MichaelBerkowski is correct, if you did want to do this with Laravel array helpers, it would be:

$names = array_fetch($tags, 'name');

which would give you an array, not a string (so having echo $name like in your example would give you an Array to string conversion error).

You may be confusing the syntax you were attempting for when you have a multidimensional associate array as in the Laravel docs for array_fetch: http://laravel.com/docs/helpers

Incidentally, if $tags was a Collection, you could do: $tags->lists('name');

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I did initially get the array to string error - the collections class looks useful - thanks for advice

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.