0

I am having the following friendDetails array output,

Array ( [0] => Array ( [id] => 1 [first_name] => Aruun [last_name] => Sukumar [photo] => jpg )

[1] => Array
    (
        [id] => 2
        [first_name] => senthilkumar
        [last_name] => Kumar
        [photo] => jpg
    )

) I use the following piece of code to to get final output

  foreach($friendDetails as $value){
       array_push($friendList, $value[id].".".$value[photo]."-".$value[first_name]." ".$value[last_name]);
  }

Final output will be,

Array
(
    [0] => 1.jpg-Aruun Sukumar
    [1] => 2.jpg-senthilkumar Kumar
    [2] => 18.jpg-senthilkumar sugumar
)

Here I am getting Notice Error with exact output. What i done wrong on the code? Is there any other way to get Final Output ?

0

6 Answers 6

1

You get the notice error as you are not putting the keys of your array in quotes.

It should be:

foreach($friendDetails as $value){                       
    array_push($friendList, $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);                    
}

see http://php.net/manual/en/language.types.array.php

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

Comments

1

You need to put quotes around your key identifiers:

$value['id'] . "." . $value['photo']

etc. See "Why is $foo[bar] wrong?" at http://php.net/manual/en/language.types.array.php

Comments

1

Try this you will get both key and value:

  foreach ($friendDetails as $key_name => $key_value) {
   print "Key = " . $key_name . " Value = " . $key_value . "<BR>";
  }

Comments

1

Two things:

  • You need to put quotes (") around the array keys in the array_push (i.e. $value["id"])
  • Make sure that you define $friendList as an array before the foreach.

A working example:

<?php

$friendDetails = array(
  array(
    'id' => 1,
    'first_name' => 'Aruun',
    'last_name' => 'Sukumar',
    'photo' => 'jpg'
  ),
  array(
    'id' => 2,
    'first_name' => 'senthilkumar',
    'last_name' => 'Kumar',
    'photo' => 'jpg'
  )
);

$friendList = array();

foreach($friendDetails as $value){                       
 array_push($friendList, $value["id"].".".$value["photo"]."-".$value["first_name"]." ".$value["last_name"]);                    
}

print_r($friendList);

?>

Comments

0

Use quotations around your array values:

foreach($friendDetails as $value){                         
   array_push($friendList, $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);                     
} 

Comments

0
$friendList = array();    
foreach($friendDetails as $key=> $value){                       
        $friendList[] =  $value['id'].".".$value['photo']."-".$value['first_name']." ".$value['last_name']);                    
    }

Comments

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.