Reputation: 53
I get an array from json but i don't know how to get bookTitle and url. How can i read this items in the arrays ?
$data = file_get_contents('C:\Users\rsanchro\Desktop\ftp brasil4-9-2015\moderna_plus-historia\MPCH-C05-P1-1.0.1-ZIP-PT\resource.json');
$datos = json_decode($data, true);
print_r($datos);die;
Results:
Array
(
[topBarColor] => #000000
[bookId] => 2aef55bf3a9dec377b4f16a8048c7f84
[bookTitle] => História
[unitId] => MPCH-C05-P1
[unitTitle] => 108-127-MPCH-C05-P1-M
[unitResources] => Array
(
[0] => Array
(
[type] => 03
[url] => resources/mphis_c06av_escultura_ontem.mp4
[urlToShowInsideUnit] => #/compId/id0a33b2a0471e0f444bc0a88b5bb55714
[title] => A escultura ontem e hoje
[page] => 123
)
)
)
Upvotes: 3
Views: 73
Reputation: 1131
I'd also recommend taking a look at the jsonselect library. It provides an intuitive selection syntax that is modeled after CSS selector syntax.
JSON Select project site: http://jsonselect.org/#overview
PHP implementation: https://github.com/observu/JSONselect-php
Upvotes: 0
Reputation: 1265
Try this
echo "Title:".$datos['bookTitle']; //To print book title
//To print url
if(is_array($datos['unitResources']) && count($datos['unitResources']) > 0){
foreach($datos['unitResources'] as $key => $value){
echo "URL:".$value['url'] ;
}
}
Upvotes: 0
Reputation: 127
if you are having only single data use the following
$bookTitle=$datos['bookTitle'];
$url=$datos['unitResources'][0]['url'];
If you have more than one data in the array then use the following
foreach($datos as $dato){
$bookTitle=$dato['bookTitle'];
$urls=array();
foreach($datos['unitResources'] as $url){
$urls[]=$url['url'];
}
echo '<br>Book Title :'.$bookTitle;
echo '<br>urls :';
foreach($urls as $url){
echo $url.'<br>';
}
}
For your example array which you mentioned. you can use the first method. but since you are working with array better use the 2nd method.
Upvotes: 0
Reputation: 2530
Use this
print_r(utf8_decode($datos['bookTitle']));
if(is_array($datos['unitResources']) && count($datos['unitResources']) > 0){
foreach($datos['unitResources'] as $key => $value){
print_r($value['url']);
}
}
Upvotes: 1