Solved

Assigning associative array key in an array in just one loop.

Posted on 2012-03-13
2
164 Views
Last Modified: 2012-04-02
How can I combine these two loops in to one loop?

for ($i = count($objs)-1; $i >= 0; $i--) { 

$tempObject = new Article();
$tempObject -> title=$objs[$i];
$tempObject -> author="My Author: " . $i;

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

}

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

Open in new window

0
Comment
Question by:glynco
2 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37715480
What are you trying to accomplish here?  It looks like you have two different arrays in play.  Unless these arrays are hundreds of thousands of elements long, there is no economy in combining these loops and plenty of potential for making a programming error.

If you have some test data, please post it, thanks.
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 37715497
You could try this:

for ($i = count($objs)-1; $i >= 0; $i--) {

$tempObject = new Article();
$tempObject -> title=$objs[$i];
$tempObject -> author="My Author: " . $i;

$articles[get_class($tempObject)][$tempObject -> title] = $tempObject;

}
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

773 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