0

When print_r($my_array);

Array
(
    [0] => Array
        (
            [value] => num1
            [label] => 
        )

    [1] => Array
        (
            [value] => num2
            [label] => 
        )

    [2] => Array
        (
            [value] => num3
            [label] => 
        )

)

Now I wonder how to create a new array variable to hold only 1 level with the following structure. e.g:

if print_r($new_array), it will show:

Array
(
    [0] => num1

    [1] => num2

    [2] => num3

)
1
  • what you have tried show your code? Commented Jul 3, 2014 at 6:25

6 Answers 6

2

Try foreach() and store value in new array

foreach($arr as $v) {
  $newarr[] = $v['value']; 
}
print_r($newarr);
Sign up to request clarification or add additional context in comments.

Comments

1
$new = array_map(function($element){ return $element['value'] ; }, $array);

Comments

0

Try with foreach of $my_array and assign value to $new_array variable like

$new_array = array();
foreach($my_array as $array){
  $new_array[] = $array['value'];
}
print_r($new_array);

Comments

0

Loop thru the array as follows and push the desired elements into a new array.

$new_array = array();

for($i=0;$i<count($my_array);$i++) {
    array_push($new_array, $my_array[$i]['value']);
}

print_r($new_array);

Comments

0

below can work:

foreach($my_array as $k => $v){

$new_array[] = $v['value'];

}

print_r($new_array);

Comments

0
$new_array = array();
foreach ($my_array as $key => $value) {
    $new_array[] = $value['value'];
}
print_r($new_array);

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.