Please provide help on how to write array values if I have array like this:
Array (
[937245328] => $0.79
[310776983] => $0.53
[720315389] => $0.39
[310800933] => $0.30
[1011934667] => $0.28
[1576813623] => $0.21
[926978479] => $0.19
[1011934570] => $0.14
[937244096] => $0.14
[310777321] => $0.13
[384801319] => $0.13
[519987816] => $0.12
[992123310] => $0.11
)
I would like to print this array out somelike this:
937245328: $0.79
310776983: $0.53
720315389: $0.39
and so on... Thanks for any help.
if your array is called
$myArray
Then you can print it out as follows:
<?php
foreach ($myArray as $key => $value)
{
echo $key.": ".$value."<br>";
}
?>
Let me know if that worked for you! :)
You can print the array (the key and the value) using foreach loop.
<?php
$dolla = Array (
'937245328' => '$0.79',
'310776983' => '$0.53',
'720315389' => '$0.39'
);
foreach($dolla as $key => $value) {
echo $key.": ".$value."<br>";
}
?>
Related
I need to random a single element from the array. I have code ;
if (isset($_POST['losuj'])) {
$arr = [
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/2.jpg',
'mienso3' => 'skiny/2.jpg',
'mienso4' => 'skiny/2.jpg',
'mienso5' => 'skiny/2.jpg',
'Hasasdasd' => 'skiny/2.jpg',
];
foreach($arr as $key => $value) {
$keys = array_rand( $arr, 1);
echo $keys;
}
}
And its didnt working. Any tips ?
You can use array_keys to get the keys in a indexed array.
The just use array_rand just like you did to pick one and echo the $arr associative key.
$keys = array_keys($arr);
$random = $keys[array_rand($keys,1)];
Echo $random . " => " . $arr[$random];
https://3v4l.org/miacb
Just use array_rand($arr,1) without foreach loop
With PHP we can use the function array_rand()
More information can be found at:
http://php.net/manual/en/function.array-rand.php
https://www.w3schools.com/php/func_array_rand.asp
$arr = [
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/2.jpg',
'mienso3' => 'skiny/2.jpg',
'mienso4' => 'skiny/2.jpg',
'mienso5' => 'skiny/2.jpg',
'Hasasdasd' => 'skiny/2.jpg',
];
$randomEntry = array_rand($arr, 1);
use this without the loop
$key = array_rand($arr);
echo $arr[$key];
full example
$arr = [
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/3.jpg',
'mienso3' => 'skiny/4.jpg',
'mienso4' => 'skiny/5.jpg',
'mienso5' => 'skiny/6.jpg',
'Hasasdasd' => 'skiny/7.jpg',
];
$key = array_rand($arr);
echo $key;
echo $arr[$key];
You should print array result like below,
$rand_keys = array_rand($arr, 1);
echo $arr[$rand_keys[0]] . "\n";
Are the '.jpg' files meant to all be the same except for one? Because randomly choosing between 7 files when 6 are the same is going to return the same file more than often.
$rand_keys = array_rand($arr);
echo $arr[$rand_keys];
<?php
$input = array(
'chleb' => 'skiny/1.jpg',
'mienso' => 'skiny/2.jpg',
'mienso2' => 'skiny/3.jpg',
'mienso3' => 'skiny/4.jpg',
'mienso4' => 'skiny/5.jpg',
'mienso5' => 'skiny/6.jpg',
'Hasasdasd' => 'skiny/7.jpg',
);
foreach($input as $key => $value) {
$keys = array_rand( $input, 1);
echo $input[$keys];
}
?>
For some reason when I put this array through foreach it does not work properly.
What am I doing wrong?
array (size=2)'Final' => array (size=1)'sender' => array (size=1)'asd' => array (size=2)...'Hos' => array (size=1)'sender' => array (size=1)'asd' => array (size=2)...
foreach($sent_app_groups['title'] as $k => $v) {
$pill_title = array_search($sent_app_groups['title'][$k],$sent_app_groups['title']);
echo $pill_title;
}
The result should be:
Final Hos
But I always get:
Final Final
I guess this is the right way to do it, silly me :P
But still I don't get it, shouldn't it do the same thing with array_search?
foreach($sent_app_groups['title'] as $k => $v) {
$pill_title = $k;
echo $pill_title;
}
I'm using PDO::FETCH_ASSOC and i'm trying to get data in mysql. This is my sql data:
Array
(
[lessons_media_id] => 1
[lessons_id] => 1
[lessons_media_photo] => http://link1
)
Array
(
[lessons_media_id] => 2
[lessons_id] => 1
[lessons_media_photo] => http://link2
)
I use foreach:
<?php
foreach($data as $key => $value){
echo $value["lessons_media_photo"];
}
?>
Outputs:
http://link1 http://link2
I wanna use one by one. How can I do this ?
Examples:
<p>http://link1</p>
<img src="http://link2">
Try This:
<?php
foreach($data as $key => $value){
echo '<p>'.$value["lessons_media_photo"].'</p>';
echo '<img src = '.$value["lessons_media_photo"].'>';
}
?>
Have an array for a ranking script.
Some times the key will be the same. They are numeric.
When the sort is ran, only non-like values are echoed.
Can't figure out the fix.
$list = array( $value1 => 'text', $value2 => 'text', $value3 => 'text');
krsort($list);
foreach ($list as $key => $frame) {
echo $frame;
}
If you assign two values to the same key in an array, the first value will be overridden by the second. You'll therefore end up with only one value for that key in the array.
To resolve this, I'd suggest to change your array structure like this:
<?php
$list = array( $key1 => array($key1member1, $key2member2),
$key2 => array($key2member1),
$key3 => array($key3member1, $key3member2, $key3member3) );
krsort($list);
foreach ($list as $key => $frames) {
foreach ($frames => $frame) {
echo $frame;
}
}
?>
Going by what you wrote in the comments to this question and my other answer, I'd recommend to switch keys and values.
<?php
$list = array( "frame1" => 4, "frame2" => 2, "frame3" => 99, "frame4" => 42 );
arsort($list);
foreach ($list as $frame => $ranking) {
echo $frame;
}
?>
I'm doing some work with a Google Analytics class. I get output as below:
Array
(
[20090401] => Array
(
[ga:pageviews] => 5000
[ga:visits] => 2500
)
[20090402] => Array
(
[ga:pageviews] => 5000
[ga:visits] => 2500
)
etc. How do I get the data to display in a table with the first column showing the date? ie the key for each array element.
like this:
20090401-----5000-----2500
Try this:
<?php
foreach ($report as $date=>$item) {
print($date.'-----'.$item['ga:pageviews'].'-----'.$item['ga:visits']);
}
?>
The piece you were missing was assigning the optional variable for the key in your foreach.
I'm not quite sure what you're asking but maybe this would help...
<?php
foreach($array as $key => $value)
{
echo $key . " => " . $value;
}
?>
Untested, but here's the idea...
foreach ( $report as $item => $data ) {
echo implode( '-----', array( $item, $data['ga:pageviews'], $data['ga:visits'] ) );
}
foreach($array AS $date => $data){
echo '
<tr>
<td>'.$date.'</td>
<td>'.$data['ga:pageviews'].'</td>
<td>'.$data['ga:visits'].'</td>
</tr>';
}
Check the php documentation about the foreach construct if you did not know about it.
The PHP function array_keys might help you out too: http://us.php.net/manual/en/function.array-keys.php