php - Convert Multidimensional Array to Single -
i using yii2 php framework , have query in accessing users in db:
$allusersquery = new query; $allusersquery->select(['_id'])->from('user')->where([ 'parent' => new \mongoid($session['company_owner']) ]); $allusers = $allusersquery->all();
when var_dump
$allusers
array, gives me output:
array (size=5) 0 => array (size=1) '_id' => object(mongoid)[147] public '$id' => string '55d5a227650fc90c35000044' (length=24) 1 => array (size=1) '_id' => object(mongoid)[148] public '$id' => string '55d5a22a650fc90c35000047' (length=24) 2 => array (size=1) '_id' => object(mongoid)[149] public '$id' => string '55d5a22a650fc90c3500004a' (length=24) 3 => array (size=1) '_id' => object(mongoid)[150] public '$id' => string '55d5a22b650fc90c3500004d' (length=24) 4 => array (size=1) '_id' => object(mongoid)[151] public '$id' => string '55d5a22b650fc90c35000050' (length=24)
it's multidimensional array. have searched , tried several solutions give me result:
array (size=1) '_id' => object(mongoid)[147] public '$id' => string '55d5a227650fc90c35000044' (length=24)
here few solutions i've tried failed work:
1.
function array_flatten($allusers) { if (!is_array($allusers)) { return false; } $allusersresult = array(); foreach ($allusers $key => $value) { if (is_array($value)) { $allusersresult = array_merge($allusersresult, array_flatten($value)); } else { $allusersresult[$key] = $value; } } return $allusersresult; }
2.
array_reduce($allusers, 'array_merge', array());
3.
$allusers = $allusers[0];
my expected output is:
array('value1', 'value2', 'value3');
how make work?
you can iterate through array , collect data.
have edited function:
function array_flatten($allusers) { if (!is_array($allusers)) { return false; } $allusersresult = array(); foreach ($allusers $key => $value) { if (!is_array($value)) { return false; } $allusersresult[] = $value["_id"]->{'$id'}; } return $allusersresult; }
Comments
Post a Comment