glynco
asked on
PHP array_push with string key
I have this initilize
how can I assign the string key to this
$articles['Article'] = array('some string key' );
how can I assign the string key to this
array_push($articles[get_class($tempObject)], $tempObject);
Maybe something like this?
$object_name = get_class($object);
$articles[$object_name] = $object;
I do not know where array_push() fits into the equation ;-)
ASKER
here is the Article.php class
class Article {
public $title;
public $author;
public $pubdate;
public $description;
public $url;
public function __construct($title, $author, $pubdate, $description, $url)
{
$this->title = $title;
$this->author = $author;
$this->pubdate = $pubdate;
$this->description = $description;
$this->url = $url;
}
}
?>
ASKER
I need a key string to search for the article object.
Something like "Jungle Book" as the key string to search for, and the object is class Book.php which has titles, author name, etc.
http://php-html.net/tutorials/model-view-controller-in-php/
Something like "Jungle Book" as the key string to search for, and the object is class Book.php which has titles, author name, etc.
return array(
"Jungle Book" => new Book("Jungle Book", "R. Kipling", "A classic book."),
"Moonwalker" => new Book("Moonwalker", "J. Walker", ""),
"PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", "")
);
http://php-html.net/tutorials/model-view-controller-in-php/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I just added the class and it works.
foreach ($articles['Article'] as $my_object)
{
$title = (string)$my_object->title;
$my_associative_array[$tit le] = $my_object;
}
foreach ($articles['Article'] as $my_object)
{
$title = (string)$my_object->title;
$my_associative_array[$tit
}
ASKER
Thanks...
you show your -
$articles['Article']
as being set to an array with One string in it, and in your "assign the string key" question you have it with - $tempObject you do Not say, but from the name it is an Object, not a string. What is it you need to do, How do we know how to make - $tempObject - a string, if we do not know what Object it is? Or what you may need in the -
$articles['myClass'] array sub array ??