Forum Moderators: coopster

Message Too Old, No Replies

Pushing data into an array

         

itledi

7:55 pm on Jan 27, 2008 (gmt 0)

10+ Year Member



Hello,

I'm trying to push some data into an array.

Right now the following code works:

array_push($news,$title,$link,$description,$author);

However, I would like to push the data into an array with labled keys. Something like:

array_push($news,"title"=>$title,"link"=>$link,"description"=>$description,"author"=>$author);

Any ideas?

jatar_k

8:10 pm on Jan 27, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't believe array_push works like that, I thought it only took values as it "treats the array like a stack"

there are other ways if you are just creating an array

are you doing this in a loop?

coopster

8:50 pm on Jan 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>"treats the array like a stack"

That's correct, and further on in the manual page [php.net] there is a note:

Has the same effect as:
<?php
$array[] = $var;
?>
repeated for each var

So, you can do this and get the same effect:


$news["title"] = $title;
$news["link"] = $link;
$news["description"] = $description;
$news["author"] = $author;

Psychopsia

8:53 pm on Jan 28, 2008 (gmt 0)

10+ Year Member



Hello, try this:

$data = array(
'title'=> $title,
'link' => $link,
'description' => $description,
'author' => $author
);
array_push($news, $data);