I have a JSON file I want to fill in a table, but can't quite figure out how to retrieve the data inside and loop it through an array to get in a table I made.
This is my JSON file: test.JSON:
{
"data":{
"Chair":{
"id":24,"key":"Chair","name":"Chair","title":"oak home made"
},
"Table":{
"id":37,"key":"Table","name":"Table","title":"round white table"
},
"Closet":{
"id":18,"key":"Closet","name":"Closet","title":"big and red"
},
"Sofa":{
"id":110,"key":"Sofa","name":"Sofa","title":"room for five persons"
}
},
"type":"furniture","version":"1.1.0"
}
Then with PHP I used this: test.PHP:
$url = 'test.json';
$result=file_get_contents($url);
$decoded=json_decode($result, true);
var_dump($decoded);
This is what I get:
array(3) { ["data"]=> array(4)
{
["Chair"]=> array(4)
{ ["id"]=> int(24) ["key"]=> string(5) "Chair" ["name"]=> string(5) "Chair" ["title"]=> string(13) "oak home made" }
["Table"]=> array(4)
{ ["id"]=> int(37) ["key"]=> string(5) "Table" ["name"]=> string(5) "Table" ["title"]=> string(17) "round white table" }
["Closet"]=> array(4)
{ ["id"]=> int(18) ["key"]=> string(6) "Closet" ["name"]=> string(6) "Closet" ["title"]=> string(11) "big and red" }
["Sofa"]=> array(4)
{ ["id"]=> int(110) ["key"]=> string(4) "Sofa" ["name"]=> string(4) "Sofa" ["title"]=> string(21) "room for five persons" }
}
["type"]=> string(9) "furniture" ["version"]=> string(5) "1.1.0"
}
Then I am trying to retrieve "Chair", "Table", "Closet" and "Sofa", but I don't know how to do it. When I try this:
foreach ($decoded as $key => $value) {
echo "key: ".$key;
echo "</br></br>";
echo "value: ".$value;
echo "</br></br>";
return;
}
I get:
key: data
value: Array
Can someone help me get "Chair", "Table", "Closet" and "Sofa", including each of thoses' id, key, name and title?
I have received guides, but they won't help me because I feel like this type of "array" is different then the links for guides I receive.
Thanks!
You are looping through the array on the top most level, while you want to be a level lower than that to be able to loop through all the elements of data, like this:
foreach ($summonerDecoded2['data'] as $key => $value)
{
echo "key: ".$key."</br>";
echo "value id: ".$value['id']."</br>";
echo "value key: ".$value['key']."</br>";
echo "value name: ".$value['name']."</br>";
echo "value title: ".$value['title']."</br>";
}
You can just do this:
$url = 'test.json';
$result=file_get_contents($url);
$decoded = json_decode($result, true);
foreach ( $decoded["data"] as $key => $value) {
echo $value[ "key" ];
echo "<br />";
//You can access data by:
/*
echo $value[ "id" ];
echo $value[ "key" ];
echo $value[ "name" ];
echo $value[ "title" ];
*/
}
This will result to:
Chair
Table
Closet
Sofa
Related
I have a SQL Query result (array): "title", "content" and "name"
Here is my var_dump:
array(28) {
[0]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(43) "https://www.wrike.com/open.htm?id=440908999"
["name"]=>
string(14) "Christian Wahl"
}
[1]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(5) "MySQL"
["name"]=>
string(14) "Christian Wahl"
}
[2]=>
array(3) {
["title"]=>
string(4) "Test"
["content"]=>
string(3) "PHP"
["name"]=>
string(14) "Christian Wahl"
}
[3]=>
array(3) {
["title"]=>
string(4) "Test"
["content"]=>
string(3) "PHP"
["name"]=>
string(14) "Christian Wahl"
}
[4]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(7) "content"
["name"]=>
string(9) "Elena Ott"
}
(I cut off the end of the array to make it a little clearer to see)
These are Tasks who are assigned to a User.
Now i want to output the "Name" (panel-heading)
and the "title" and "content" (panel-body).
It should look smth like this but for each given name:
how it should look like
I tried to find a solution on my own, but without success :(
I hope u can help me?
thx a lot
-Taddl
just loop on your data and render it
$data = [];
foreach ($databaseResult as $row) {
$data[$row['name']][] = $row;
}
foreach($data as $name => $stuff) {
echo $name . '<br>';
foreach($stuff as $row) {
echo $row["title"] . ':' . $row["content"] . '<BR>';
}
}
You can use foreach loop. As example:
foreach($yourarrayvariable as $data)
{
echo "<tr>";
echo "<td class='heading'>".$data['name']."</td>";
echo "<div class='content'>";
echo "<td>".$data['title']."</td>";
echo "<td>".$data['content']."</td>";
echo "</div>";
echo "</tr>";
}
And create your desired template look classes as per your need inside foreach.
I am aware of this question, but I have an additional one to search for an array-key. Take a look at this:
array(2) {
[0]=>
array(2) {
["name"]=>
string(6) "Text 1"
["types"]=>
array(3) {
[0]=>
string(7) "Level 1"
[1]=>
string(14) "something else"
[2]=>
string(15) "whatisearchfor1"
}
}
[1]=>
array(2) {
["name"]=>
string(6) "Text 2"
["types"]=>
array(3) {
[0]=>
string(7) "Level 2"
[1]=>
string(14) "something else"
[2]=>
string(15) "whatisearchfor2"
}
}
}
This snippet...
echo array_search("Text 2", array_column($response, "name"));
...gives me a 1 for the second array-key, in which the term was found.
But how do I receive the global array-key (0 or 1), if I search for whatisearchfor2, which is stored in the multi-array "types"?
echo array_search("whatisearchfor2", array_column($response, "types"));
...doesn't work.
In your case array_column($response, "types") will return an array of arrays. But to get "global array-key (0 or 1), if you search for whatisearchfor2" use the following approach with array_walk:
$key = null; // will contain the needed 'global array-key' if a search was successful
$needle = "whatisearchfor2"; // searched word
// assuming that $arr is your initial array
array_walk($arr, function ($v,$k) use(&$key, $needle){
if (in_array($needle, $v['types'])){
$key = $k;
}
});
var_dump($key); // outputs: int(1)
I am currently getting the same $val results but the $key is working fine. What am i doing wrong?
foreach($awards['award_title'] as $key) {
foreach($_POST['award_title_new'] as $val)
$award_title_trans[$key] = $val;
}
update_option('award_title', $award_title_trans );
$awards array:
["award_title"]=>
array(2) {
[0]=>
string(7) "testnew"
[1]=>
string(5) "newti"
}
and the $POST is an input array name='award_title_new[]'
What i am expecting is to save the $awards value as a key and the $POST as the value:
array(2) {
["testnew"]=>
string(3) "345"
["newti"]=>
string(3) "345"
}
This did the trick!
array_combine($awards['award_title'], $_POST['award_title_new']);
array(2) {
["testnew"]=>
string(3) "hello"
["newti"]=>
string(3) "bye"
}
i have a question here...
i have an array from mysql like this...
array(3) {
[0]=>
array(4) {
["id"]=>
string(1) "1"
["name"]=>
string(8) "Delivery"
["namaShipp"]=>
string(3) "JNE"
["price"]=>
string(5) "30000"
}
[1]=>
array(4) {
["id"]=>
string(1) "3"
["name"]=>
string(12) "Installation"
["namaShipp"]=>
string(7) "Kudamas"
["price"]=>
string(1) "0"
}
[2]=>
array(4) {
["id"]=>
string(1) "5"
["name"]=>
string(12) "Installation"
["namaShipp"]=>
string(5) "MTECH"
["price"]=>
string(1) "0"
}
}
and then i want output it to a simple table.. and i dont know how..
So the result is grouped by ['name'] key.
My question is how to output that array to become like this
-----------------------------
INSTALLATION (get from ['name'])
----------------------------
MTECH - 0 <---- MTECH is get from ['namaShipp'] and 0 is from ['price']
KUDAMAS - 0
-----------------------------
DELIVERY
-----------------------------
JNE - 30000
Anybody can help please?? i will appreciate that..
i've just very confused about thisss...
thanks before
Something like this:
$current=false;
foreach($result as $item)
{
if($item['name']!==$current)
{
echo '
---------------------
'.$item['name'].'
---------------------';
$current=$item['name'];
}
echo '
'$item['namaShipp'].' - '.$item['price'];
}
Try this (assuming your array you showed is $processedArray):
$processedArray = array();
foreach ($mainArray as $innerArray) {
$processedArray[$innerArray['name']][] = $innerArray;
}
foreach ($processedArray as $name => $itemsArray) {
echo '----<br />'.$name.'<br />---';
foreach ($itemsArray as $item) {
echo $item['namaShipp'].' - '.$item['price'].'<br />';
}
}
Transform your array! Create an array of arrays of records:
$list=array();
foreach($data as $item)
$list[$item['name']][]=$item;
foreach($list as $groupname=>$records){
echo '--------------- '.$groupname.' ------------';
foreach($records as $item){
//output one $item as before...
}
}
$result = $mysql->callSP('STOREDPE1',$in);
$arr_tmp=array();
foreach ($result['record'] as $rows) {
echo "one value";
if(!empty($rows)){
echo "Two Value";
$arr_tmp[$rows['field1']] = $rows['field2'];
}
}
return $arr_tmp;
When I say var_dump($result) it has two values in array. But when I Execute arr_tmp it is returned with one value.
out put of ``var_dump($result)`
["param"]=>
array(4) {
["whcode"]=>
string(5) "001"
["mode"]=>
string(1) "A"
["stock_type"]=>
string(4) "AAA"
["process_name"]=>
string(7) "AAAA"
}
["record"]=>
array(2) {
[0]=>
array(3) {
["Field1"]=>
string(5) "value1"
["Field2"]=>
string(1) "CCC"
["Field3"]=>
string(4) "BCC"
}
[1]=>
[0]=>
array(3) {
["Field1"]=>
string(5) "value1"
["Field2"]=>
string(1) "CCC"
["Field3"]=>
string(4) "BCC"
}
}
}
output of var_dump (arr_tmp)
[1]=>
[0]=>
array(3) {
["Field1"]=>
string(5) "value1"
["Field2"]=>
string(1) "CCC"
["Field3"]=>
string(4) "BCC"
}
Both the array values seems to be same
I have the values overwriting in the array
Very hard to understand and read with the bad formatting, please take care to post it with proper formatting.
I think the answer is this:
$result = $mysql->callSP('STOREDPE1',$in);
$arr_tmp=array();
foreach ($result['record'] as $rows) {
echo "one value";
if(!empty($rows)){
echo "Two Value";
$arr_tmp[][$rows['field1']] = $rows['field2'];
}
}
var_dump($arr_tmp);
That should store both sets of data, you just needed to make it a multi-dimensional array. That is, if that is your question and I did not mis-read it through that garbled text above.
Update
This option is not recommended better to learn how to use arrays, simply posted for an example of usage:
$result = $mysql->callSP('STOREDPE1',$in);
$arr_tmp=array();
$i=0;
foreach ($result['record'] as $rows) {
echo "one value";
if(!empty($rows)){
echo "Two Value";
$arr_tmp[][$rows['field1'] . "_$i"] = $rows['field2'];
}
$i++;
}
var_dump($arr_tmp);