Create table stats from Json - php

I have a Json result like so
{"goals":["1","1"],"minutes":["12","34"],"player":["1","1"]}
And i am trying to create a table with rows displaying each stat like so
Goals Minutes Player
1 12 1
1 34 1
I currently have this code written up and although i am getting teh data its not quite working the way i want it to. Im not so clued up on Json and the displaying of it as you can see.
<?php
$jsonresult = $this->item->results;
$phpArray = json_decode($jsonresult, true);
foreach ($phpArray as $key => $value) {
foreach ($value as $k => $v) {
?>
<tr><td><?php echo $v; ?></td></tr>
<?php }} ?>
Cheers
Jonny

<?php
$json = '{"goals":["1","1"],"minutes":["12","34"],"player":["1","1"]}';
$array = json_decode($json,true);
print_r($array);
print "Goals, Minutes, Player\n";
foreach($array['goals'] as $key => $value) {
print $value . ','
. $array['minutes'][$key] . ','
. $array['player'][$key] . "\n";
}
Output
Goals, Minutes, Player
1,12,1
1,34,1

Related

PHP Print array values without all the extra "=>" stuff

I have an array and I want to simply print out a list of all the values. Not sure why I can't find the answer to this. I have tried "var_dump" and "Var_export", "TRUE" and "FALSE". Here is my code:
$var = var_export($xyz,TRUE);
print "$var";
But it outputs this:
array (
0 => '700',
1 => '750', etc
I just want this:
700
750
<?php
$a = array (700, 750);
foreach($a as $key => $value)
{
echo $value. "<br>";
}
?>
You can easily print the value of an array using the foreach loop. Here I gave an example for your better help.
foreach ($xyz as $val) {
echo $val . '<br>';
}
?
Have you tried iterating into $xyz using a foreach loop ?
foreach($xyz as $value) {
print "$value \n" }
The \n being a new line.
edit : identical to #PHPNoob's answer
Looks like you are attempting to get the values from an array.
Using a Foreach will loop through the array apply the code to each value until the array is complete.
foreach($xyz as $value){
echo $value . "<br/>";
}

Combine 2 Arrays In Foreach in PHP

I would like to combine these two foreach statements together. I've seen a few solutions around here, but nothing really works for me.
This is my username list from database.
$digits = [1,2,3,4];
$results = $db->table($usernames)
->where('memberID', $mID)->limit(10)
->getAll();
foreach ($results as $result) {
echo $result->userName;
}
I tried this:
$combined = array_merge($digits, $results);
foreach (array_unique($dogrularVeSiklar) as $single) : { ?>
{
echo $single.'<br>';
echo $results->userName;
},
}
You don't show what $dogrularVeSiklar is or where you get it, but as an example; combine into $key => $value pairs and foreach exposing the key and value:
$combined = array_combine($digits, $results);
foreach ($combined as $digit => $result) {
echo $digit . '<br>' . $result;
}
foreach operates on only one array at a time.
The way your array is structured, you can use array_combine() function to combine them into an array of key-value pairs then foreach that single array

SELECT array from mysql table and print

I have a column in my table that contain an array like:
1,4,2,8,4,5,7
How to select these numbers and print them separately?
Item: 1
Item: 4
Item: 2
Item: 8
Item: 4
Item: 5
Item: 7
What I have tried:
$result = mysql_query('SELECT * FROM test');
$arr = array();
while(($row = mysql_fetch_array($result))) {
$arr = array_merge($arr , explode(',', $row['item']));
echo "Item: " . $arr . "<br>";
}
Result:
Item: Array
Item: Array
Item: Array
Item: Array
Item: Array
Item: Array
Item: Array
Can anyone help me?
Your biggest problem is trying to echo the whole array at once rather than echoing an index of the array at a time.
Unless you plan on using all the rows again after the while loop, you don't need the array_merge so take that out. What you need is a foreach inside the while loop to loop through the array created by explode.
while(($row = mysql_fetch_array($result)))
{
$arr = explode(',', $row['item']);
foreach ($arr as $key => $value)
{
echo "Item [$key]: " . $value . "<br>";
}
}
(And you really should switch off of mysql_ to PDO or mysqli_ since mysql_ is deprecated.)
try:
$arr = explode(',', $row['item']);
foreach($arr as $single){
echo "item: $single";
}
You are almost there. I hope this gives the result you expect. In your question it is not quite clear how exactly the data is stored.
If all the data is in one row, this will do it:
$result = mysql_query('SELECT item FROM test');
$arr = array();
$row = mysql_fetch_array($result);
$arr = explode(',', $row['item']);
foreach ($arr as $item) {
echo "Item: " . $item . "<br>";
}

POST Array through JSON foreach

The question:
I have this array coming through a POST field
{"name":"sample","email":"a#sample.co.uk","comments":"test"}
I want to split it apart and run it through an array, so the end result would be
name sample
email a#sample.co.uk
comments test
What I have tried is this:
$a = $_POST['rawRequest'];
$a = json_encode($a);
foreach ($a as $k => $v) {
echo "\$a[$k] => $v <br />";
}
But it doesn't do anything, however when I test it with this variable (over using the POST)
$a = array("name" => 1,"email" => 2,"sample" => 3);
It works as expected.
Trying to understand what is going on
It is obviously because what I am dealing with here is two different types of array. However after endless google'ing I can't find anywhere which explains the difference (of the arrays below basically). So +1 to an explination which makes my relatively newbie mind understand what is happening and why it is wrong
{"name"=>"sample","email"=>"a#sample.co.uk"=>"comments":"test"}
{"name":"sample","email":"a#sample.co.uk","comments":"test"}
$aa Isn't a array, is a JSON:
$a = $_POST['rawRequest'];
$aa = json_encode($a);
Thus, you can't use foreach in $aa.
If you want to decode a json string into an array instead of an object, use the 'Array' flag.
$array = json_decode($json_string, true);
Try as
$data = '{"name":"sample","email":"a#sample.co.uk","comments":"test"}';
$json = json_decode($data,true);
foreach($json as $key=>$val){
echo $key." - ".$val;
echo "<br />";
}
Check the output here
http://phpfiddle.org/main/code/ytn-kp0
You have done as
echo "\$a[$k] => $v <br />";
This would output as
$a[name] => sample
"$a" will be considered as string
You can do the way you are doing but you need to change the echo something as
echo $k ."=>" .$v. "<br />";
Since you are looping the array using foreach and $k will contain the key of the array and $v will be the value !!
I have Json decoded the encoded array and looped it through a foreach below with comments explaining what each part does.
/* The Json encoded array.*/
$json = '{"name":"sample","email":"a#sample.co.uk","comments":"test"}';
/* Decode the Json (back to a PHP array) */
$decode = json_decode($json, true);
/* Loop through the keys and values of the array */
foreach ($decode as $k => $v) {
$new_string .= $k . ' | ' . $v . '<br/>';
}
/* Show the result on the page */
echo $new_string;
The above code returns the following;
name | sample
email | a#sample.co.uk
comments | test
If you want to access the array values one by one you can also use the following code.
/* The Json encoded array.*/
$json = '{"name":"sample","email":"a#sample.co.uk","comments":"test"}';
/* Decode the Json (back to a PHP array) */
$decode = json_decode($json, true);
echo $decode['name'];//returns sample
echo $decode['email'];//returns a#sample.co.uk
echo $decode['comments'];//returns test

Duplicate array values not working with Simple HTML DOM

I'm working with Simple HTML DOM like this:
foreach($html->find('img', 18) as $d) {
echo $d->outertext;
}
Now I want to implement an array of variables, in this case images, so I did:
$img=array(
"img"=>"18",
"img"=>"21"
);
foreach($img as $x=>$x_value)
{
$d = $html->find($x, $x_value);
echo $d->outertext;
}
The problem is that Simple HTML DOM is only returning the last image in array, which is number 21. What do I have to do to make it return everything in the array?
It's because both items in your $img array has the same key. foreach doesn't recognize them as two seperate items because both keys are img.
Example code to demonstrate:
$test = array(
"key" => 1,
"key" => 2
);
echo "Length of array: " . count($test) . "\n\n";
echo "Items in array:\n";
foreach($test as $key => $value) {
echo "$key => $value\n";
}
Outputs:
Length of array: 1
Items in array:
key => 2

Categories