Suganya Rajasekar
Suganya Rajasekar

Reputation: 684

How do I get the array values from two arrays by using keys

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

Upvotes: 1

Views: 96

Answers (6)

Sabin Chacko
Sabin Chacko

Reputation: 761

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..

Upvotes: 1

Indrajit
Indrajit

Reputation: 405

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>'; }

Upvotes: 1

Manjeet Barnala
Manjeet Barnala

Reputation: 2995

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 
)

Upvotes: 1

Edson Horacio Junior
Edson Horacio Junior

Reputation: 3143

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}";
}

Upvotes: 1

Jerodev
Jerodev

Reputation: 33216

You can use a simple for loop to do this

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

Upvotes: 1

Mureinik
Mureinik

Reputation: 312267

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

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

Upvotes: 4

Related Questions