0

Below is my result array:

Array
(
    [0] => Array
    (
        [id] => 3
        [name] => test
    )

    [1] => Array
    (
        [id] => 4
        [name] => Balikavadhu
    )
)

From above array, I want to generate a new array as below:

array(3,4) // where 3 and 4 are id values of respective array

Any help or quick answer will be appreciated.

Thanks in advance !

3 Answers 3

2

Yet another option:

 $newArray = [];

 foreach ($arrayResults as $result) {
  $newArray[] = $result['id'];
 }
Sign up to request clarification or add additional context in comments.

Comments

0

Put your output array into an array called $arr. Then do this :

$newArr = array();
$id = 0;
foreach ($arr as $val)
{
   $newArr[$i] = $val['id'];
   $id++;
}

2 Comments

can we do same with array_push function?
Can be done. This is one way of doing logically. Yours is fine too.
0

You could do something like

$newArray = [];

foreach ($arrayResults as $result) {
  array_push($newArray, $result['id']);
}

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.