2

I have 2 kinds of ARRAYS

<?php
    $array1 = array("Car", "House", "Money");
    $array2 = array("John", "Peter", "Mary");

foreach ($array1 as $a1) {
    for($i = 0; $i < count($array2); $i++) {
        if ($a1 === end($array1 )) {
          echo $array2[$i].' has '.$a1.'.<br>';
        }
        else {
          echo $array2[$i].' has '.$a1.',<br>';
        }
    }
}
?>

But the output is like this

John has Car,
Peter has Car,
Mary has Car,
John has House,
Peter has House,
Mary has House,
John has Money.
Peter has Money.
Mary has Money.

What I want is like this

John has Car,
Peter has House, 
Mary has Money.

is there any other way?

except for calling the specific value of an array like this

$array1[0]."has ".$array2[0].",<br>"
$array1[1]." has ".$array2[1].",<br>" 
$array1[2]." has ".$array2[2].".<br>"

if I need to use "break;" where should I put it?

TIA

2
  • is the arrays fixed to 3 element? Commented Mar 12, 2019 at 7:33
  • No. The data in the DB will be stored in the array1 but the data in the array2 is fixed Commented Mar 12, 2019 at 7:35

5 Answers 5

5

Please check my answer helpful for you.

<?php
    $array1 = array("Car", "House", "Money");
    $array2 = array("John", "Peter", "Mary");

for($i = 0; $i < count($array2); $i++) {

          echo $array2[$i].' has '.$array1[$i].'.<br>';

    }

?>

and output as below:

John has Car. Peter has House. Mary has Money.

Sign up to request clarification or add additional context in comments.

Comments

2

You don't need to loop twice. You can simply iterate over the list of people and use the given key to access the corresponding object:

$objects = ['Car', 'House', 'Money'];
$people = ['John', 'Peter', 'Mary'];

foreach ($people as $i => $person) {
  echo $i > 1 ? ' and ' : '', $person, ' has ', $objects[$i];
}

Demo: https://3v4l.org/Fa45t

Note that this assumes the two arrays have the same size. You can use array_key_exists (or isset in this case) to make sure, otherwise.

Updated after your edit. This handles the case where there are more people than objects:

$objects = ['Car', 'House', 'Money'];
$people = ['John', 'Peter', 'Mary', 'Paul'];

foreach ($people as $i => $person) {
  if (!array_key_exists($i, $objects)) {
    break;
  }
  echo ($i > 0 ? ',<br>' : ''), $person, ' has ', $objects[$i];
}

Demo: https://3v4l.org/Hd3e0

3 Comments

I did some changes. What if the element of the array is not fixed to 3? Thank you for your answer. I'm currently trying to fixed your code to my code to see if it works.
after tweaking your code to fit mine. it finally worked!. Thank you
@JerickArcega No problem. I've just updated my answer with a working example where there are more objects than people.
2

You can simplify your loop as you want to iterate through both arrays at the same time. So, assuming $array1 and $array2 have the same number of values, you can use something like this. By comparing the index with the number of elements in the array we decide whether to output "while", "and" or nothing after each phrase.

foreach ($array2 as $key => $value) {
    if (!isset($array1[$key])) continue;
    echo "$value has $array1[$key]";
    echo $key < count($array2) - 2 ? " while " : ($key < count($array2) - 1 ? " and " : "");
}

Output:

John has Car while Peter has House and Mary has Money

Update

Based on OPs edits, the code can be simplified to this, which includes a check that a corresponding object exists for each person (the code above has been similarly edited):

foreach ($array2 as $key => $value) {
    if (isset($array1[$key])) echo "$value has $array1[$key]";
}

Demo on 3v4l.org

2 Comments

I did some changes. What if the element of the array is not fixed to 3? Thank you for your answer. I'm currently trying to fixed your code to my code to see if it works
@JerickArcega I've made changes to cope with the situation where there might be more people than objects. But I'm glad to see you've already got Jeto's answer to work for you.
1

You can also below solution

$output_string = '';
 for($i = 0; $i < count($array1); $i++) {
    for($j = 0; $j < count($array2); $j++) {
        if($i == $j){
            $output_string .=  $array2[$i]." has ".$array1[$i];
        }
    }
    if(!empty($output_string)){
        if($i == 0){
            $output_string .= " while  ";
        }else{
            if($i<(count($array1) -1)){
                $output_string .= " and  ";
            }            
        }             
    }
}
echo $output_string;

1 Comment

I did some changes. Thank you for your answer. I'm currently trying to fixed your code to my code to see if it works
0

Drop the for loop inside your foreach loop like this:

foreach ($array1 as $key => $a1) {
  echo ($key === 1 ? " while " : ($key > 1 ? " and " : "")) . "$array2[$key] has $a1";
}

1 Comment

This is pretty much an exact copy of @Jeto answer.

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.