1

My arrays are

 $name=>
    Array
        (
            [0] => General
            [1] => General
            [2] => Outdoors
            [3] => Dining
            [4] => Dining
            [5] => Kitchen
            [6] => Kitchen

        )

 $key1=>
   Array
        (
            [0] => 1
            [1] => 2
            [2] => 7
            [3] => 11
            [4] => 12
            [5] => 17
            [6] => 18
        )

Array function

 foreach ($key1 as $key => $value1) {
                    foreach ($name as $key => $value) {

                        echo $value "=>" $value1 ;
                        //echo "$value1";
                    }
                }

Here I would like to print the values by using the same keys

if $name having the index as [0] and my $key1 also take the [0] value

i.e: my result should be in the form of

General => 1
General => 2
Outdoors => 7
Dining => 11
Dining => 12
Kitchen => 17
Kitchen => 18
2
  • foreach ($name as $key => $value) { echo $value "=>" $key1[$key];} Commented Mar 4, 2016 at 12:06
  • each foreach will be multiplied to the number of results in nested setting. so, in your code you'll have 36 outputs. Just use foreach once. Commented Mar 4, 2016 at 12:10

6 Answers 6

4

You only need to iterate one array, not both of them:

foreach ($name as $key => $name_value) {
    echo "$name_value => " . $key1[$key];
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a simple for loop to do this

for ($i = 0; $i < count($name); $i++) {
    echo $name[$i] . '=>' . $key[$i]
}

Comments

1

The problem with your code is you're using the same variable $key for both foreachs, so the last one overwrites the value.

foreach ($key1 as $key => $value1) {
    foreach ($name as $key => $value) {

        echo $value "=>" $value1 ;
        //echo "$value1";
    }
}

You could make things easier by combining those two arrays, making $name array be the keys and $key1 array be the values

$newArray = array_combine($name, $key1);
foreach ($newArray as $name => $key) {
    echo "{$name} =>{$key}";
}

2 Comments

Here if the keys having same name then it took only the last value:i.e:General=>2 and for Dining => 12 and for Kitchen => 18..But I need all the values
Thats right, I forgot about that detail, you should than use @Mureinik 's code.
1

This will work for you

<?php 
$a1= array('General','Outdoors','Dining ');
$a2= array('1','2','3');
$newArr=array();
foreach($a1 as $key=> $val)
{
    $newArr[$a2[$key]]= $val;
}
echo "<pre>"; print_r($newArr); 
?>

output

Array
(
    [1] => General
    [2] => Outdoors
    [3] => Dining 
)

Comments

1

I am afraid this wont be possible if you want the output as associative array as same key name in an associative array is not allowed. It would be always overwritten if you are dealing with the associative arrays.

Although you may have something like this:

array_map(function($key, $val) {return array($key=>$val);}, $name, $key1)

Output:

Array ( [0] => Array ( [General] => 1 ) [1] => Array ( [General] => 2 ) [2] => Array ( [Outdoors] => 7 ) [3] => Array ( [Dining] => 11 ) [4] => Array ( [Dining] => 12 ) [5] => Array ( [Kitchen] => 17 ) [6] => Array ( [Kitchen] => 18 ) ).

But if you want the output in string format It is possible.

for ($i = 0; $i < count($key); $i++) { echo $name[$i] . '=>' . $key[$i].'<br>'; }

Comments

1

Just change the foreach as follows...

foreach ($key1 as $key => $value1) {
                   echo $name[$key] ."=>". $value1."<br>";
            } 

replace the <br> with \n if you're running through the linux terminal. Also don't miss the '.' operator to concatenate the string..

Nested foreach won't do what you need... Good luck..

Comments

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.