Try if it helps:
foreach($something AS $key => $row)
{
$output[$key]['name'] = $row["name"];
$output[$key+1]['points'] = $row["points"];
}
print_r($output);
Question
See the attached example that i use to build my array
foreach($something AS $key => $row)
{
$output[] = array("name"=>$row["name"], "points"=>$row["points"]);
}
print_r($output);
Here's the output:
Array
(
[0] => Array
(
[name] => Mark
[points] => 1
)
[1] => Array
(
[name] => Sara
[points] => 2
)
[2] => Array
(
[name] => Jack
[points] => 3
)
)
What i'm trying to do is moving $row["points"] to next array element to get this output:
Array
(
[0] => Array
(
[name] => Mark
[points] =>
)
[1] => Array
(
[name] => Sara
[points] => 1
)
[2] => Array
(
[name] => Jack
[points] => 2
)
)
I don't care if there's some data loss or if my [points] => 3 goes in a new array. I just have to programmatically move $row["points"] always to next element. I'm playing with next() function with no success and also with $key+1 which i'm sure i can't use to achieve the result.
Is it possible to make it on top while i'm building the array or am i forced to move the element later with a separate function? In other words what would you do?
Solution 2
Try if it helps:
foreach($something AS $key => $row)
{
$output[$key]['name'] = $row["name"];
$output[$key+1]['points'] = $row["points"];
}
print_r($output);
OTHER TIPS
Try This:
$output = array();
$something = array(
'0' => array(
'name' => 'Mark',
'points' => 1,
),
'1' => array(
'name' => 'Sara',
'points' => 2,
),
'2' => array(
'name' => 'Jack',
'points' => 3,
)
);
$point = '';
foreach($something AS $key => $row)
{
$output[$key] = array("name"=>$row["name"], "points"=>$point);
$point = $row['points'];
}
echo '<pre>';
print_r($output);
Use:
foreach($array as $key => $each)
{
$array[0]['points'] = "";
$array[$key]['name'] = $each["name"];
if(isset($array[$key+1])) $array[$key+1]['points'] = $each["points"];
}
print_r($array);