Solved

PHP array_push with string key

Posted on 2012-03-12
7
573 Views
Last Modified: 2012-03-13
I have this initilize

$articles['Article'] = array('some string key' );

Open in new window


how can I assign the string key to this

array_push($articles[get_class($tempObject)], $tempObject);

Open in new window

0
Comment
Question by:glynco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 34

Expert Comment

by:Slick812
ID: 37711068
greetings glynco, , sorry, but I do not understand what you need to end up with in your $articles  array ? ?

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  ??
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 37712108
Maybe something like this?
$object_name = get_class($object);
$articles[$object_name] = $object;

Open in new window

I do not know where array_push() fits into the equation ;-)
0
 

Author Comment

by:glynco
ID: 37713446
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;
    } 


}
?>

Open in new window

0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:glynco
ID: 37713458
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.

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", "")  
        );

Open in new window


http://php-html.net/tutorials/model-view-controller-in-php/
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37714027
Something like "Jungle Book" as the key string to search for,...

The array you have shown above has "Jungle Book" as an array key.  In case this is what you're looking for, there is no search needed -- you simply address the object by array notation like this
$jb_object = $my_array["Jungle Book"];

Open in new window

But if you do not have array keys assigned to the data elements you want, you can use an iterator to locate the elements by title and assign the array keys like this
foreach ($my_array as $my_object)
{
    $title = (string)$my_object->title;
    $my_associative_array[$title] = $my_object;
}
$jb_object = $my_associative_array["Jungle Book"];

Open in new window

0
 

Author Comment

by:glynco
ID: 37715172
Thanks. I just added the class and it works.


foreach ($articles['Article'] as $my_object)
{
    $title = (string)$my_object->title;
    $my_associative_array[$title] = $my_object;
}
0
 

Author Closing Comment

by:glynco
ID: 37715174
Thanks...
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question