Get value from Array inside of an Array - php

I have the following string output if I run print_r($val):
{"next_offset":-1,"records":[{"id":"e3266222-5389-11ed-ab30-0210c01ad3d2","name":"That is a nice name"}]}
Now, I need the value of attribute "id". Sounds simple but I'm not able getting there.

Does something like this work?
I'm assuming $val is a json string.
<?php
$val = "{\"next_offset\":-1,\"records\":[{\"id\":\"e3266222-5389-11ed-ab30-0210c01ad3d2\",\"name\":\"That is a nice name\"}]}";
$val = json_decode($val);
print_r($val->records[0]->id);
?>

Related

Getting elements from an array with contents from decode json

It's sort of hard to describe the question in the title, but I will explain it in the body. So I have this example JSON (It's similar to the one I have):
[{"AssetId":234234,"Name":"Test1"},{"AssetId":53453,"Name":"Test2"}]
So basically I want to get the AssetId element from the first array of the main array. I am calling this in PHP, but it isn't working
$array = json_decode("[{"AssetId":234234,"Name":"Test1"},{"AssetId":53453,"Name":"Test2"}]");
echo $array[0][0]
So I guess that would be the first array in the main array to get AssetId of the element with the Name of Test1. Any help on getting it?
Try replacing the double quotes with single quotes, your attempt will lead to parse error of unexpected T_STRING
Instead of this
$array = json_decode("[{"AssetId":234234,"Name":"Test1"},{"AssetId":53453,"Name":"Test2"}]");
^
Try this:
$array = json_decode('[{"AssetId":234234,"Name":"Test1"},{"AssetId":53453,"Name":"Test2"}]');
foreach($array as $arr){
echo $arr->AssetId;
}
what about echo $array[0]->AssetId
edit :
$json = json_decode('[{"AssetId":234234,"Name":"Test1"},{"AssetId":53453,"Name":"Test2"}]');
echo $json[0]->AssetId;
works here

db table names as alias in php GET-paramter?

I am looking for a possibility to pass db table name and column name via php and GET parameter.
I have a data grid with following structure:
table_name1.column1, table_name2.column1, table_name2.column1.
There is a search function for the grid, where I need those parameters.
From the url "?table_name1.column1=22" I am getting through the $_GET only table_name1_column1=22
How would you solve that?
Encode the variable with base64 and decode before you use.
I know that's dirty.
But php variables doesn't support periods (dots)
This is a documented feature of PHP. Its basically because PHP cannot have variable names with dots in them.
$x.y = 1 // is an invalid variable name
MANUAL Convert dots to _ in GET & POST
You can use serialize function of php to pass the text "table_name1.column1" in url. But for that you need to do few coding to get the result. Below I have given code which you can use:
Page1.php
<?php
$test = serialize('table_name1.column1=11::table_name2.column2=22');
?>
<a href='Page2.php?qs=<?php echo $test?>'>test</a>
Use this $test to pass as query string. In above example, I am passing on anchor tag click.
Page2.php
use following code to Unserialize the query string and use the values.
<?php
$test2 = unserialize($_GET['qs']);
$ex = explode('::',$test2);
$new_arr = array();
foreach($ex as $val)
{
$ex2 = explode('=',$val);
$new_arr[$ex2[0]] = $ex2[1];
}
echo $new_arr['table_name1.column1']; //print 11
echo $new_arr['table_name2.column2']; //print 22
?>
Hope this will help you :)
You need to seperate each parameter with &.
So: ?table=table_name1&column=column1

php issue accessing array

I have the following code :
$results = $Q->get_posts($args);
foreach ($results as $r) {
print $r['trackArtist'];
}
This is the output :
["SOUL MINORITY"]
["INLAND KNIGHTS"]
["DUKY","LOQUACE"]
My question is, if trackArtist is an array, why can't I run the implode function like this :
$artistString = implode(" , ", $r['trackArtist']);
Thanks
UPDATE :
Yes, it is a string indeed, but from the other side it leaves as an array so I assumed it arrives as an array here also.
There must be some processing done in the back.
Any idea how I can extract the information, for example from :
["DUKY","LOQUACE"]
to get :
DUKY, LOQUACE
Thanks for your time
It's probably a JSON string. You can do this to get the desired result:
$a = json_decode($r['trackArtist']); // turns your string into an array
$artistString = implode(', ', $a); // now you can use implode
It looks like it's not actually an array; it's the string '["DUKY","LOQUACE"]' An array would be printed as Array. You can confirm this with:
var_dump($r['trackArtist']);
To me content of $r['trackArtist'] is NOT an array. Just regular string or object. Instead of print use print_r() or var_dump() to figure this out and then adjust your code to work correctly with the type of object it really is.

Help Decoding JSON

I'm trying to get the "screenshotUrls" string from this piece of json:
$request_url = 'http://itunes.apple.com/search?term=ibooks&country=us&entity=software&limit=1';
$json = file_get_contents($request_url);
$decode = json_decode($json, true);
echo $decode['results'][0]['screenshotUrls'];
But I get only text "Array"
What have I done wrong?
Try
var_dump($decode['results'][0]['screenshotUrls']);
IF you get 'Array' output by PHP that means that you're trying to echo an actual array (or the string 'Array'...). That means you need to get a specific index value.
Since $decode['results']['0']['screenshotUrls'] is an array, if you want just a string (say, delimited by commas), you could use
echo implode(",", $decode['results']['0']['screenshotUrls']);
This will iterate over the array, and return a comma-separated string of all the URLs.
Do a var_dump($decode['results']['0']['screenshotUrls']). You'll find that the ['screenshotUrls'] is actually an Array, containing one or more URLs (hence the plural 'urls' in its name).
There's nothing wrong with your code so far, but $decode['results'][0]['screenshotUrls'] is an array of all the URLs to screenshots. To go through each one individualy, you need to do:
forearch ($decode['results'][0]['screenshotUrls'] as $url) {
// Do stuff here
}

PHP: problem inserting array inside an array

I have a script that is using the google charts API and the gChart wrapper.
I have an array that when dumped looks like this:
$values = implode(',', array_values($backup));
var_dump($values);
string(12) "8526,567,833"
I want to use the array like this:
$piChart = new gPieChart();
$piChart->addDataSet(array($values));
I would have thought this would have looked like this:
$piChart->addDataSet(array(8526,567,833));
Howerver when I run the code it creates a chart with only the first value.
Now when I hardcode the values in instead I get each value in the chart.
Does anyone know why it's acting this way?
Jonesy
I think
$piChart->addDataSet(array_values($backup));
// or just: $piChart->addDataSet($backup); depends on $backup
should do it.
$values only contains a string. So if you do array($values), you create an array with one element:
$values = "8526,567,833";
print_r(array($values));
gives
Array
(
[0] => 8526,567,833
)
array(8526,567,833) would be the same as array_values($backup) or maybe even just $backup, that depends on the $backup array.
Looks like you want to use $backup instead of $values as $values is the imploded string... and since 8526,567,833 isn't a valid number, it parses 8526 and leaves the rest alone.

Categories