I'm trying to integrate a 3rd party service with a Restful APi (first time doing something like this). I have managed to send the query, get a response from them, and converted it into a JSON array using:
$company_json = file_get_contents($3rdparty_url);
$company_array = json_decode($company_json, true);
The problem I'm having is getting the values out of this complex array.
This is what the array looks like:
{
"response":{
"pagination":{
"next_url":"http:\/\/someurl.org\/companies?limit=25&offset=25",
"total":33
},
"data":[
{
"id":"09934451",
"name":"Acme Incorporated"
},
{
"id":"00435820",
"name":"Acme Group Limited"
},
{
"id":"06841797",
"name":"Acme Ltd"
}
]
},
"request_id":"570bf0ca96a63"
}
I'm able to get the 'name' values out of the array by traversing it with some PHP like this:
foreach ($company_array as $data1 => $value1) {
foreach ($value1 as $data2 => $value2) {
foreach ($value2 as $data3 => $value3) {
foreach ($value3 as $data4 => $value4) {
if ($data4 == 'name') {
print $value4;
}
}
}
}
}
But what I really want to do is grab the values and put them into some sort of format like:
<ul>
<li id="09934451">Acme Incorporated</li>
<li id="00435820">Acme Group Limited</li>
<li id="06841797">Acme Ltd</li>
</ul>
From my searching around it looked like I would be able to do it with something like this, but it doesn't work. The $value4['id'] & $value4['name'], only print out the first letter of the values as if I were using $value4[0]:
foreach ($company_array as $data1 => $value1) {
foreach ($value1 as $data2 => $value2) {
foreach ($value2 as $data3 => $value3) {
foreach ($value3 as $data4 => $value4) {
print '<li id="' . $value4['id'] . '">' . $value4['name'] . '</li>';
}
}
}
}
I don't think I can be too far off but it's starting to do my head in.
Your over complicating it, it's very simple...
foreach ($company_array['response']['data'] as $data) {
echo '<li id="' . $data['id'] . '">' . $data['name'] . '</li>';
}
Related
First of all I have searched for the similar threads on StackOverflow like this one
$myvar = array ("key_name" => array("tom", "an", "bob"),
"key_age" => array("1", "10", "12")
);
I have tried lot of things but I couldn't
foreach($myvar as $i){
foreach ($i as $key => $value) {
echo print_r($i);
}
I am trying to get "key_name" and loop through it
<?php
$myvar = array (
"key_name" => array("tom", "an", "bob"),
"key_age" => array("1", "10", "12")
);
foreach ($myvar['key_name'] as $value) {
echo $value;
}
Result:
tomanbob
https://3v4l.org/tEFvS
If you want to loop through both:
foreach ($myvar as $sub_array) {
foreach ($sub_array as $value) {
echo $value;
}
}
Result:
tomanbob11012
https://3v4l.org/cMFUh
Check out, http://php.net/manual/en/control-structures.foreach.php for info on using foreach
You can use array_walker. So you can do something like this :
array_walk($myvar,function($sub_items,$key){
echo "Key is >> " . $key . "\n";
foreach($sub_items as $item){
echo $item . "\n";
}
echo "------------ \n ";
});
Note:
I put an echo with a new line to understand how to implement that!
So here's the thing, I get this json from a url ( in my context i get it from a url, but let's say here I write my json in a variable :
$file = '[
{"status": "5.4.1","email": "dddddd#exelcia-it.com"},
{"status": "5.4.1",, "email": "sksksksk#exelcia-it.com"}
]'
Then I do $json = json_decode($file,true);
And I want to get all the emails so I do :
foreach ($json as $key => $value) {
echo $value["email"]. "<br>";
}
But what I also need, is to return something like that from the loop (only for one property):
"email = dddddd#exelcia-it.com".
So I need to also get the name of the property but I can't figure this out.
I tried
foreach($json as $key => $propName){
echo $key.'<br>';
}
But I just get the index (0,1,...), not what I want.
Thanks!
You have to loop each json row, this should works:
$file = '[{"status": "5.4.1","email": "dddddd#exelcia-it.com"},{"status": "5.4.1", "email": "sksksksk#exelcia-it.com"}]';
$json = json_decode($file,true);
foreach($json as $row)
{
foreach($row as $key => $value)
{
echo "<b>".$key."</b>".':'.$value.'<br>';
}
}
Use this loop to get key and value pairs together.
foreach($data as $row)
{
foreach($row as $key => $val)
{
echo $key . ': ' . $val;
echo '<br>';
}
}
Ok thanks that's what I needed !
For me I just need the email properties and values, so I do :
foreach($json as $row)
{
foreach($row as $key => $value)
{
if($key=='email'){
echo "<b>".$key."</b>".':'.$value.'<br>';
}
}
}
Awesome ! thanks !
I need to work out how i can get showBtn(3) to match up against the first result in every other key.
ShowBtn/3
btnMenulink/101
btnArticleLink/2
btnPhone/036244789
btnUrl/
btnName/Office
PHP:
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$array = json_decode($jsonresult,true);
foreach ($array as $key => $value) {
foreach ($value as $next_key => $next_value) {
echo $key . ":" . $next_key . ":" . $next_value . "\n";
}
}
I want this:
if (showBtn == 3) {
echo '<a href='tel:btnPhone'>btnName</a>';
}
the result would be
Office Mobile
I almost have it!
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$parsed = json_decode($jsonresult,true);
echo 'Showbtn: '.$parsed['showBtn'][0].' Phone: '.$parsed['btnPhone'][0].' Name: '.$parsed['btnName'][0];
echo '<hr/>Showbtn: '.$parsed['showBtn'][1].' Phone: '.$parsed['btnPhone'][1].' Name: '.$parsed['btnName'][1];
Now i just gotta get that [0] [1] into the loop somehow
I have it but can it be done better?
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$parsed = json_decode($jsonresult,true);
$i=0;
foreach ($parsed as $key => $value) {
if ($parsed['showBtn'][$i] == 3) {
echo 'Showbtn: '.$parsed['showBtn'][$i].' Phone: '.$parsed['btnPhone'][$i].' Name: '.$parsed['btnName'][$i].'<hr/>';
}
$i++;
}
This'll work for you. You need to work it as this way
$jsonresult = '{"showBtn":["3","3"],"btnMenuLink":["101","101"],"btnArticleLink":["2","2"],"btnPhone":["036244789","0404256478"],"btnURL":["",""],"btnName":["Office","Mobile"]}';
$array = json_decode($jsonresult,true);
foreach($array['showBtn'] as $key => $value){
if($value == 3){
echo ''.$array['btnName'][$key].'<br>';
}
}
Output:
Office
Mobile
Trying to get to grips with PHP, but I have absolutely no idea how to do this.
I want to take this array:
$things = array('vehicle' => array('car' => array('hatchback', 'saloon'),'van','lorry'),
'person' => array('male', 'female'),
'matter' => array('solid', 'liquid', 'gas'),
);
and turn it into this into something like this in HTML:
Vehicle
Car
Hatchback
Saloon
Van
Lorry
Person
Male
Female
Matter
Solid
Liquid
Gas
Tried a number of solutions from searching, but cannot get anything to work at all.
What you are looking for is called Recursion. Below is a recursive function that calls itself if the value of the array key is also an array.
function printArrayList($array)
{
echo "<ul>";
foreach($array as $k => $v) {
if (is_array($v)) {
echo "<li>" . $k . "</li>";
printArrayList($v);
continue;
}
echo "<li>" . $v . "</li>";
}
echo "</ul>";
}
Try something like:
<?php
function ToUl($input){
echo "<ul>";
$oldvalue = null;
foreach($input as $value){
if($oldvalue != null && !is_array($value))
echo "</li>";
if(is_array($value)){
ToUl($value);
}else
echo "<li>" + $value;
$oldvalue = $value;
}
if($oldvalue != null)
echo "</li>";
echo "</ul>";
}
?>
Code source: Multidimensional array to HTML unordered list
I have a multidimensional array in PHP, something that looks like:
array(array(Category => Video,
Value => 10.99),
array(Category => Video,
Value => 12.99),
array(Category => Music,
Value => 9.99)
)
and what I would like to do is combine similar categories and output everything into a table, so the output would end up being:
<tr><td>Video</td><td>23.98</td></tr>
<tr><td>Music</td><td>9.99</td></tr>
Any suggestions on how to do this?
EDIT:
I can have these in two different arrays if that would be easier.
A simple loop will do:
$array = [your array];
$result = array();
foreach ($array as $a) {
if (!isset($result[$a['Category']])) {
$result[$a['Category']] = $a['Value'];
} else {
$result[$a['Category']] += $a['Value'];
}
}
foreach ($result as $k => $v) {
echo '<tr><td>' . htmlspecialchars($k) . '</td><td>' . $v . '</td></tr>';
}
$result = array();
foreach ($array as $value) {
if (isset($result[$value['Category']])) {
$result[$value['Category']] += $value['Value'];
} else {
$result[$value['Category']] = $value['Value'];
}
}
foreach ($result as $category => $value) {
print "<tr><td>$category</td><td>$value</td></tr>";
}