-2

Using PHP I am try to echo out the first item from an array...

Array
(
    [docs] => Array
        (
            [0] => Array
                (
                    [imgurl] => http://www.example.com/image1.jpg
                )

            [1] => Array
                (
                    [imgurl] => http://www.example.com/image2.jpg
                )

            [2] => Array
                (
                    [imgurl] => http://www.example.com/image3.jpg
                )

            [3] => Array
                (
                    [imgurl] => http://www.example.com/image4.jpg
                )

        )

)

I am using the following PHP to attempt to display the first item...

echo $array['docs'][0]['imgurl'];

But it is giving me the error...

Warning: Illegal string offset 'docs'

Can anyone show me what I am doing wrong?

fightstarr20
  • 11,682
  • 40
  • 154
  • 278

2 Answers2

2

@fightstarr20, Your array format is not correct. I just correct it and then tried and it works fine.

            <?php
                $array = Array('docs' => Array
                        (
                            0 => Array
                                (
                                    'imgurl' => 'http://www.example.com/image1.jpg'
                                ),
                            1 => Array
                                (
                                    'imgurl' => 'http://www.example.com/image2.jpg'
                                ),
                            2 => Array
                                (
                                    'imgurl' => 'http://www.example.com/image3.jpg'
                                ),
                            3 => Array
                                (
                                    'imgurl' => 'http://www.example.com/image4.jpg'
                                ),
                        )
                );
                echo $array['docs'][0]['imgurl'];
                ?>

Output is:- http://www.example.com/image1.jpg

halfer
  • 19,824
  • 17
  • 99
  • 186
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Well just to write code which works doesn't solves OP's problem at all! I could write: `echo "hey";` and say it works, is this also an answer? NO! – Rizier123 Mar 10 '15 at 12:16
  • Actually it does solve the question....read his code again. – Eda190 Mar 10 '15 at 12:17
  • @Eda190 No it does not! Just rewriting code which we don't even see doesn't solve anything and with no explanation, which we can't give to OP because we don't see his code, it's even worse! *Delete your code and write: echo "test"; and the code will work* <- Is this an answer? NO it's not maybe the code works, but it doesn't fix the problem from OP – Rizier123 Mar 10 '15 at 12:21
  • OP's syntax is OBVIOUSLY wrong, and this answer was even marked as correct by OP. I don't know what you're talking about here, because he didn't provide alternate solution. He provided correction. – Eda190 Mar 10 '15 at 12:24
  • 1
    Because this doesn't fix OP's real problem! Here he just provides working code. But it doesn't explain what was wrong with OP's code, Oh! yes you can't because you don't see it. I also can write an answer with: `Your code is wrong, use echo "xy"; and it works!` <- Do I get upV for this too? – Rizier123 Mar 10 '15 at 12:26
0

Since you have not given the full code I assume you have the following array:

$array = array('docs'=>array(
     '0'=>array('imgurl'=>'http://www.example.com/image0.jpg'),
     '1'=>array('imgurl'=>'http://www.example.com/image1.jpg'),
     '2'=>array('imgurl'=>'http://www.example.com/image2.jpg'),
     '3'=>array('imgurl'=>'http://www.example.com/image3.jpg'),
));

Then you can access the imgurl as:

echo $array['docs'][0]['imgurl'];

You can test the above code here

Chinmay Waghmare
  • 5,368
  • 2
  • 43
  • 68