1

this is output from array.

[25.1] => Super Cylinder|2080
[25.2] => Main Post Powered Rotation|10780
[36.1] => Electric H-Frame Trolley|16630
[36.2] => Air Powered H-Frame Trolley|11028

I want output like this how can I get this output. How can set associated keys value into main key that will be round number.

[25]=> Super Cylinder|2080,Main Post Powered Rotation|10780
[36]= > Electric H-Frame Trolley|16630, Air Powered H-Frame

How Can I get the array values like this.

2 Answers 2

2

Short solution using regular foreach loop:

$arr = [
    "25.1" => "Super Cylinder|2080",
    "25.2" => "Main Post Powered Rotation|10780",
    "36.1" => "Electric H-Frame Trolley|16630",
    "36.2" => "Air Powered H-Frame Trolley|11028"
];

$result = [];
foreach ($arr as $k => $v) {
    $rounded = round((int) $k);
    (isset($result[$rounded]))? $result[$rounded] .= ', '. $v : $result[$rounded] = $v;
}

print_r($result);

The output:

Array
(
    [25] => Super Cylinder|2080, Main Post Powered Rotation|10780
    [36] => Electric H-Frame Trolley|16630, Air Powered H-Frame Trolley|11028
)
Sign up to request clarification or add additional context in comments.

Comments

1

you could merge it manually as:

<?php

$arr = array(
    "25.1" => "Super Cylinder|2080",
    "25.2" => "Main Post Powered Rotation|10780",
    "36.1" => "Electric H-Frame Trolley|16630",
    "36.2" => "Air Powered H-Frame Trolley|11028"
);

$ret = array();
foreach($arr as $key => $val){
    $key_new = intval($key);
    if(!isset($ret[$key_new])) $ret[$key_new] = array();
    array_push($ret[$key_new], $val);
}
//print_r($ret);

//merge the values
foreach($ret as $key => &$val){
    $val = implode(",", $val);
}
unset($val);
print_r($ret);

?>

this gives:

Array
(
    [25] => Super Cylinder|2080,Main Post Powered Rotation|10780
    [36] => Electric H-Frame Trolley|16630,Air Powered H-Frame Trolley|11028
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.