0

My Array shown as follows:

Array
(
[0] => Array
    (
        [amount_id] => 1
        [enquiry_id] => 1
        [project_id] => 1
    )

[1] => Array
    (
        [amount_id] => 4
        [enquiry_id] => 4
        [project_id] => 4
    )

[2] => Array
    (
        [amount_id] => 5
        [enquiry_id] => 5
        [project_id] => 5
    )

)

This Array can be increase. How can i get value of each 'amount_id' from this array? What function should i use? Can for each function will work?

4 Answers 4

1

Just try with:

$input  = array( /* your data */ );
$output = array();
foreach ($input as $data) {
  $output[] = $data['amount_id'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a one-liner array_walk() to print those..

array_walk($arr,function($v){ echo $v['amount_id']."<br>";});

Working Demo

1 Comment

This will print all elements, and not a specific one. I think the question is "each" element
1

Do like this in array_map or Use array_column for The version PHP 5.5 or greater

 $outputarr= array_map(function($item){ return $item['amount_id'];},$yourarr);
 print_r($outputarr);

Comments

0

Try the following, this is accessing the specific array - easier to debug later on and better speed:

$num=count($your_array);
for($i="0"; $i<$num; $i++)
    {
    echo $your_array[$i]['amount_id'];
    }

you could loop until $i < count($your_array) but it means that the count will run in every loop, for high performance sites I wouldn't do it.

you could access a specific element in a 2D array by $your_array[$the_index]['amount_id'] or other index.

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.