parsing values in a php array - php

I am new to PHP, please help me with the stuff. Below is the code written followed by its results. How can i access [count] inside [property] in most effective way.
code
echo '<pre>';
print_r($result);
echo '</pre>';
output
Array
(
[0] => Array
(
[property] => Array
(
[href] => http://www.example.com
[count] => 2
[sample] => Array
(
)
[data] => Array
(
[0] => 100002067610524
[1] => 675985591
)
[users] =>
[active] => 1
)
)
)

Not the most effective, but the only possible ;-)
echo $result[0]['property']['count'];

if you have more than one element then use this
foreach($result as $childArray)
{
echo $childArray['property']['count'];
}
if you have only one element in it then use
$array = $result[0]['property']['count'];

Related

How to loop through json array within an array php

I need help traversing an array within array, I only need to loop through certain arrays, for example, how would I just lop through the names array?
Array
(
[#total_records] => 10
[#total_matching_records] => 10
[#available_records] => 200
[#available_matching_records] => 12
[query] => Array
(
[summary] => Array
(
[emails] => Array
(
[0] => Array
(
[content] => jonathan.lyon#gmail.com
)
)
)
)
[results] => Array
(
[person] => Array
(
[#match_score] => 1
[summary] => Array
(
[names] => Array
(
[0] => Array
(
[first] => Jonathan
[last] => Lyon
[display] => Jonathan Lyon
)
[1] => Array
(
[first] => Jonathan
[last] => Jordan
[display] => Jonathan Jordan
)
)
I have tried this but can't get it to work:-
foreach($json_output['results']['person']['summary']['names'] as $key => $val) {
echo $key.": ".$val."</br>";
}
Any help would be greatly appreciated.
Thanks
Jonathan
In your example you trying to echo $key. Key in your case $key is array index (integer). Are you sure you realy need that?
You nedd to change your code to:
foreach($json_output['results']['person']['summary']['names'] as $val) {
echo $val['display']."</br>";
}
Have you tried this
foreach($json_output['results']['person']['summary']['names'] as $key => $val) {
echo $key.": ".$val['display']."</br>";
}
?
Are you getting any error output? That would help a lot. I can see also that $val in this case is an array so you don't want to echo that.

Strip a IPTC.php result into two variables

I have the following code:
<?php
require_once('IPTC.php');
$iptc = new Image_IPTC('001.jpg');
print_r($iptc);
?>
And it returns:
Image_IPTC Object ( [_sFilename] => 001.jpg [_aIPTC] => Array (
[1#090] => Array ( [0] => %G ) [2#000] => Array ( [0] => ) [2#005]
=> Array ( [0] => TITULO NO WINDOWS, TITLE NO BRIDGE ) [2#080] => Array ( [0] => pictureauthor ) [2#085] => Array ( [0] => photographer )
[2#090] => Array ( [0] => mycity ) [2#095] => Array ( [0] => ST )
[2#101] => Array ( [0] => mycountry ) [2#105] => Array ( [0] =>
IWANTTHIS1 ) [2#116] => Array ( [0] => copyrightinfo ) [2#120] => Array ( [0] => IWANTTHIS2 ) ) [_bIPTCParse] => 1
)
This is a dummy question, but how do I put texts "IWANTTHIS1" and "IWANTTHIS2" into 2 different variables to use like this:
echo "title: $variable1 <br />";
echo "descr: $variable2";
Resulting in:
title: IWANTTHIS1
descr: IWANTTHIS2
I'm pretty shure it's extremelly easy for you guys, but I'm still learning all this. I think it's an array inside an array? Can't figure it out.
Thanks.
$variable1 = $iptc->_aIPTC['2#105'][0];
$variable2 = $iptc->_aIPTC['2#120'][0];
Rodaine's answer was giving me "Fatal error: Cannot use object of type Image_IPTC as array". With some research, the final correct answer is:
$variable1 = $iptc->_aIPTC['2#105'][0];
$variable2 = $iptc->_aIPTC['2#120'][0];
I wouldn't be able to achieve it by myself at all, therefore I'm marking Rodaine's answer as correct.
Thank you very much.

PHP, how to echo specific object data from array?

I use this in wordpress:
$arr=get_post_meta($post->ID, false);
I receive this array:
Array (
[_edit_last] => Array ( [0] => 2)
[year_completed] => Array ( [0] => 2010 )
[designers] => Array ( [0] => )
[developers] => Array ( [0] => )
[producers] => Array ( [0] => )
[_edit_lock] => Array ( [0] => 1298159324 )
[name_en] => Array ( [0] => game 1)
[name_et] => Array ( [0] => game 2 )
[friends_meta] => Array ( [0] => )
)
How do I echo (no for, foreach, etc please) name_en data? Even print_r ($arr->name_en); doesn't work... I suppose it has to be something like - echo $arr->name_en[0]; ???
It is an array or arrays, so:
print_r($arr['name_en']);
or if you only want to get the data:
echo $arr['name_en'][0];
The -> operator is for accessing properties of objects.
echo $arr['name_en'][0] should work.
this should work
echo $arr[0]['year_completed'];
echo $arr[0]['designers'];
etc

How to get data in PHP array with foreach?

I have data like this and I need to get it to variable for use please suggest to me how to get them to variable
Array
(
[data] => Array
(
[0] => Array
(
[name] => xxxxxxxxxxxx
[category] => yyyyyyyy
[id] => 12345666666
[data] => ABCDE
)
[1] => Array
(
[name] => ZZZZZZZZZZZ
[category] => JJJJJJ
[id] => 88888888888888
[data] => ABCDEHIJK
)
)
)
Thank you vary much.
foreach ($array['data'] as $data) {
echo $data['name'];
echo $data['category'];
...
}
http://php.net/manual/en/control-structures.foreach.php

how to extract the exact data from an array in php

i got this array from an database...how to get the exact value attribute......
Array
(
[0] => TRowResult Object
(
[row] => tests
[columns] => Array
(
[a21ha:link_0] => TCell Object
(
[value] =>testsample
[timestamp] => 1265010584060
)
[a21ha:link_1] => TCell Object
(
[value] => example
[timestamp] => 1265092697040
)
)
)
how to get the [value] alone in php
print $myArray[0]->columns['a21ha:link_0']->value;
This would give you the first. To get all, you'd have to loop through the contents.
foreach ($myArray[0]->columns as $column) {
print $column->value;
}
Supposed your array called $array:
foreach($array as $arridx => $rowobj){
foreach($rowobj->columns as $cellid => $rowcell){
echo $rowcell->value;
}
}

Categories