Show result of multiple keys and array column in php

Show result of multiple keys and array column in php



I have an array like this:-


$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test' ,
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test1',
)
);



Now I want to show the both the amounts in an html table which user 'hello' has. I tried the following for searching it :-


$ac = array_search("hello", $str);
echo $str["$ac"];



But it doesnt work. Is there anyway to show an result like this for user 'hello' :-


1.87
9



So that I can later show in html table.




4 Answers
4



You can use array_column()


$users = array_column($str,'user'); // get all the user list from array
$search = "hello"; // user you want to search
foreach($users as $key=>$value) // iterate over user array
if($value == $search) // compare user name with search value
echo $str[$key]['amount'] .PHP_EOL; // if matched print the corresponding amount




https://eval.in/1052935





Thnx it worked :D but any ideas how to get this on html table?
– JeffB
Sep 1 at 5:22





you can use same code in HTML TABLE. make sure file extension will be .php and add tr and td correspondingly.
– Alive to Die
Sep 1 at 5:24


.php


tr


td





Ok thank you for helping :)
– JeffB
Sep 1 at 5:26





<html> <head> <title>Test</title> </head> <body> <table style="width:100%"> <tr> <th>Amount</th> </tr> <tr> <td><?php $users = array_column($str,'user'); // get all the user list from array $search = "man"; // user you want to search foreach($users as $key=>$value) // iterate over user array if($value == $search) // compare user name with search value echo $str[$key]['amount'] ; // if matched print the corresponding amount ?></td> </table> </body> </html> I tried this but doesnt show properly
– JeffB
Sep 1 at 5:30






@JeffB please ask a new question with code and HTML. and you will get answer by some-one
– Alive to Die
Sep 1 at 5:31


foreach($str as $new_str)
if($new_str['user']=="hello")
echo $new_str['amount'];
echo "<br />";




Here you can get this


$ac = array_column($record, ‘Hello’);
Print($ac);





Sorry but it doesnt work
– JeffB
Sep 1 at 5:23


<?php
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test' ,
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test1',
)
);

$key = array_keys(array_column($str, 'user'), 'hello');
foreach($key as $value)
echo $str[$value]["amount"]."<br/>";

?>





Thanks for spending you'r time but it shows [0,2] which is wrong.
– JeffB
Sep 1 at 5:31





I used to get the array index of all that containing key user="hello". I have updated just check it
– Saravanan
Sep 1 at 5:42



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)