I have an Array
Given Data
Array
(
[0] => Array
(
[name] => /w/s/wsh11-blue_main.jpg
[image_id] => L3cvcy93c2gxMS1ibHVlX21haW4uanBn
[file_path] => /w/s/wsh11-blue_main.jpg
[file_name] => wsh11-blue_main.jpg
[added_at] => September 19 2019 09:32:24
[serial_no] => 1
)
[1] => Array
(
[name] => /w/s/wsh05-purple_main.jpg
[image_id] => L3cvcy93c2gwNS1wdXJwbGVfbWFpbi5qcGc,
[file_path] => /w/s/wsh05-purple_main.jpg
[file_name] => wsh05-purple_main.jpg
[added_at] => September 19 2019 09:32:24
[serial_no] => 2
}
}
Now my problem is want to get a specific column in another array
i have tried array_column($data,'file_path')
but unfortunate i am getting value only not key
i want to get An Expect Array Like
Expected Result
Array
(
[0] => Array
(
[file_path] => /w/s/wsh11-blue_main.jpg
)
[1] => Array
(
[file_path] => /w/s/wsh05-purple_main.jpg
)
}
$arrayRec= array ();
foreach ($arrayRec as $rec){
$collection[] = array('file_path'=>$rec['file_path']);
}
echo "<pre>";
print_r($collection);
echo "</pre>";
One approach is to use a loop to build a new array with the expected data. An alternative is the array_map()
function. It iterates over each array element and collects the return values into a new array.
$data = [
[
'image_id' => 'L3cvcy93c2gxMS1ibHVlX21haW4uanBn',
'file_path' => '/w/s/wsh11-blue_main.jpg'
],
[
'image_id' => 'L3cvcy93c2gwNS1wdXJwbGVfbWFpbi5qcGc',
'file_path' => '/w/s/wsh05-purple_main.jpg'
]
];
$mapped = array_map(
static function ($file) {
return ['file_path' => $file['file_path']];
},
$data
);
var_dump($mapped);
Output:
array(2) {
[0]=>
array(1) {
["file_path"]=>
string(24) "/w/s/wsh11-blue_main.jpg"
}
[1]=>
array(1) {
["file_path"]=>
string(26) "/w/s/wsh05-purple_main.jpg"
}
}
$newArray = array();
$arr = array();
foreach($array as $key => $value){
if($key == "file_path"){
$arr[$key] = $value;
$newArray[] = $arr;
}
}
Iterate over the array, get the value from the inner array and save it in the new array.
$finalArray = [];
for($finalData as $innerArray){
$finalArray[] = array('file_path'=>$innerArray['innerArray']);
}
$finalArray
has what you need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.