arrays - Concatenate two consecutive foreach iterations in PHP? -
i have $result
variable. has following structure:
i trying array structured this: masculine => 1
, feminine => 2
.
i stuck here:
foreach ($result $row) { foreach ($row $column => $value) { echo $column . ' order ' . $value . '<br/>'; } }
the output of attempt is:
i tried using next()
function , apply $row
variabile, each iteration got 1(2) => false
.
i'd appreciate help!
you can use php builtin function array_column job.
array_column — return values single column in input array
(php 5 >= 5.5.0, php 7)
syntax -
array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )
and script -
<?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 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 $ar) { $tmp[key($ar)] = current($ar); } $arr = $tmp; } return $arr; } } ?>
hope you!!
Comments
Post a Comment