Mihai
Mihai

Reputation: 2927

Concatenate two consecutive foreach iterations in PHP?

I have the $result variable. It has the following structure: enter image description here

I am trying to get an array structured like this: masculine => 1, feminine => 2.

I am stuck here:

foreach ($result as $row)
{
   foreach ($row as $column => $value)
   {
       echo $column . ' with order ' . $value . '<br/>';
   }
}

The output of my attempt is:

enter image description here

I tried using the next() function and apply it to the $row variabile, but for each iteration I got the 1(2) => false.

I'd really appreciate your help!

Upvotes: 0

Views: 343

Answers (3)

melledijkstra
melledijkstra

Reputation: 359

I hope I understood your question, but based on the result you wanted I created this:

$result = array(
    array(
        'gender_value' => 'masculine',
        'gender_preferred_order' => 1,
    ),
    array(
        'gender_value' => 'feminine',
        'gender_preferred_order' => 2,
    )
);

$set = array();

foreach($result as $gender) {
    $set[] = array($gender["gender_value"] => $gender["gender_preferred_order"]);
}

var_dump($set); // [['masculine' => 1],['feminine' => 2]]

Upvotes: 1

Vikash Kumar
Vikash Kumar

Reputation: 1111

You can use PHP builtin function array_column to do this job.

array_column — Return the values from a single column in the input array

(PHP 5 >= 5.5.0, PHP 7)

Syntax -

array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )

And script would be like this -

<?php

$result = array(
    array(
        'gender_value' => 'masculine',
        'gender_preffered_order' => 1
    ),
    array(
        'gender_value' => 'feminine',
        'gender_preffered_order' => 2
    ),
);

$filtered = array_column($result, 'gender_preffered_order', 'gender_value');
print_r($filtered);

For more reference see the link - http://php.net/manual/en/function.array-column.php

For PHP < 5.5

<?php

if (!function_exists('array_column')) {
    function array_column($input, $column_key, $index_key = null) {
        $arr = array_map(function($d) use ($column_key, $index_key) {
            if (!isset($d[$column_key])) {
                return null;
            }
            if ($index_key !== null) {
                return array($d[$index_key] => $d[$column_key]);
            }
            return $d[$column_key];
        }, $input);

        if ($index_key !== null) {
            $tmp = array();
            foreach ($arr as $ar) {
                $tmp[key($ar)] = current($ar);
            }
            $arr = $tmp;
        }
        return $arr;
    }
}
?>

Hope this will help you!!

Upvotes: 2

MonkeyZeus
MonkeyZeus

Reputation: 20737

This might work:

$new_array = []; // Not sure if you needed this piece but I've included it :)

foreach ($result as $row)
{
   echo $row['gender_value'].' => '.$row['gender_preferred_order'].'<br>';

   $new_array[$row['gender_value']] = $row['gender_preferred_order'];
}

print_r($new_array);

I highly recommend getting acquainted with print_r()

Upvotes: 3

Related Questions