Question

I have an array like this :

[[a,b],[c,d]]

How can I delete the outer array so it becomes:

[a,b],[c,d]

I tried using unset but cannot find a solution.

i got this answer by asign arrays in loops to another variable.. so how can i access the loops outside the loop it self?

    foreach($arr3 as $key => $value)
{
    $newArr[$key] = $value;

    echo json_encode($value); // this will answer my question
}
      echo json_encode($value); // when i echo outside loop it will not display as inside loop
Was it helpful?

Solution

Simple way to do this is as below.

foreach($array as $key => $value)
{
    $newArr[$key] = $value;
}

$newArr contains new array which you are asking.

Comment Response

You can also con cat it.

$concat = "";
foreach($arr3 as $key => $value)
{
    $newArr[$key] = $value;
    $concat .= json_encode($value).',';
}
echo rtrim($concat,',');

OTHER TIPS

You should not delete the outer array. You can simply access the index of the array and cast it to another array.

As an example;

$arr = array(array('x','y'),array('z'));

You can access this with;

$arr[0];
$array2=$array[0];
$array3=$array[1];
unset($array);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top