I am having following multidimensional array. I want to convert this array in new array form as given in array 2
Array-1
Array (
[0] => Array
(
[0] => PRpriyer
[1] => job1232
)
[1] => Array
(
[0] => PRpriyer
[1] => job1234
)
[2] => Array
(
[0] => PRpriyer
[1] => job1235
)
[3] => Array
(
[0] => PRpriyer
[1] => job1236
)
[4] => Array
(
[0] => JSjais
[1] => job1232
)
[5] => Array
(
[0] => JSjais
[1] => job1234
)
)
I want new array from this format, like this: Array-2
Array (
[0] => Array
(
[0] => PRpriyer
[1] => job1232 | job1234 | job1235 | job1236
)
[1] => Array
(
[0] => JSjais
[1] => job1232 | job1234
)
)
How to convert? Please help
foreach($array1 as $value){ $tmparray[$value[0]][]=$value[1]; }
foreach($tmparray as $key=>$values){
$result[]=array($key,implode(" | ",$values));
}
print_r($result);
$array1 is the original
$newArray = array();
foreach($array1 as $value){
$newArray[$value[0]] .= $value[1]." | ";
}
newArray:
Array
(
[PRpriyer] => job1232 | job1234 | job1235 | job1236 |
[JSjais] => job1232 | job1234 |
)
I'd do the following:
function convertArray($inputArray) {
$array = array();
foreach ($inputArray as $value) {
$array[$value[0]][] = $value[1];
}
$returnArray = array();
foreach ($array as $key => $values) {
$returnArray[] = array($key, implode(" | ", $values));
}
return $returnArray;
}
$result = convertArray($inputArray);
print_r($result);
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.