3

If I have a loop like this, and an array which stores information:

$itemArray = array();
foreach ($a->getDetails() as $b) 
{
    if ($b->getValue1() !== $b->getValue2()) 
    {
        if (!array_key_exists($b->getId(), $itemArray))
        {
            $itemArray[$b->getId()] = array('name' => $b->getName(), 'age' => $b->getAge());
        }

    $personName = $itemArray[$b->getId()]['name'];
    $personAge  = $itemArray[$b->getId()]['age'];

    $content    = ('Name is: ' . $personName . ', age is: ' . $personAge);
    }
}

Right now this only outputs a single 'person' for a single value of $b which is mismatched, how would I go about storing multiple values of $b being mismatched?

I basically want the output to be something along the lines of:

Name is: Dave, age is 30.

Name is: John, age is 40.

But right now only one 'person' would get output even if there were two instances where

$b->getValue1() !== $b->getValue2()

A sample output of $a->getDetails():

array(1) {
[0]=>
  object(PersonDetail)#322 (41) {
["collItemTemplateFieldPersonValues":protected]=>
NULL
["id":protected]=>
int(2375434)
["person_id":protected]=>
int(2184229)
["person_details_id":protected]=>
int(4563874)
["person_details_type_id":protected]=>
NULL
["name":protected]=>
string(4) "Test"
["person_namecode":protected]=>
string(9) "PERSON_ID"
["person_age":protected]=>
int(30)
3
  • can you paste the "$a->getDetails()" input Commented Feb 26, 2016 at 9:56
  • can you post all the objects as well? Commented Feb 26, 2016 at 9:56
  • I posted a small sample of $a->getDetails() Commented Feb 26, 2016 at 10:02

1 Answer 1

1

You've already stored all you need in array, you've just to loop through :)

$itemArray = array();
foreach ($a->getDetails() as $b) {
    if ($b->getValue1() !== $b->getValue2()) {
        if (!array_key_exists($b->getId(), $itemArray)) {
            $itemArray[$b->getId()] = array('name' => $b->getName(), 'age' => $b->getAge());
        }
    }
}
if (count($itemArray) > 0) {
    foreach($itemArray as $item) {
        $personName = $item['name'];
        $personAge  = $item['age'] ;
        $content    = ('Name is: ' . $personName . ', age is: ' . $personAge);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

This makes sense thanks! If I wanted to say.. send an email within php using the output from the array, all in a SINGLE email, how would I create a variable to hold all the data?
@Sudoscience You have to ask a new question for this, we must keep SO clean :), also if you accept this answer, you must check it and optionally upvote it... share me your question's link and i will help you
Done! Here is the new question Link: stackoverflow.com/questions/35650419/…

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.