How to fetch data from json string using php - php

I have json string and I want to fetch product data from this string How can I achieve this. Please some one help me.
Below is my string,
{"num_rows":2,"row":{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},"rows":[{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},{"setting":"a:6:{s:4:\"name\";s:17:\"Featured Products\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"146\";}s:5:\"limit\";s:1:\"4\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"}]}

There is a mix of JSON and PHP-serialized data.
<?php
$string = '{"num_rows":2,"row":{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},"rows":[{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},{"setting":"a:6:{s:4:\"name\";s:17:\"Featured Products\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"146\";}s:5:\"limit\";s:1:\"4\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"}]}';
$dataObject = json_decode($string);
foreach($dataObject->rows as $row){
$productData = unserialize($row->setting);
print_r($productData);
}
Will result in
Array
(
[name] => Featutred
[product] => Array
(
[0] => 145
[1] => 148
)
[limit] => 5
[width] => 200
[height] => 200
[status] => 1
)
Array
(
[name] => Featured Products
[product] => Array
(
[0] => 145
[1] => 146
)
[limit] => 4
[width] => 200
[height] => 200
[status] => 1
)
NOTE: There is no error checking in code above, since it is written for your particular example. If you are not sure that your input data is correct (that is usual), you need to check if JSON is ok, object is object and also has needed properties, etc.

Related

Two Child Arrays Within One Parent Array Problem: one child array is processed after processing both child arrays have same result

I have an array for the page to handle data within shown below:
(B4:CCTB SPEC FIX) $FIXER_INPUTS reads:
stdClass Object
(
[CCTB] => stdClass Object
(
[OLD] => 90-15-6170
[NEW] => 90-15-6170
[OLD_SPEC] => stdClass Object
(
[volt] => 12 Volt
[series] => K1 Series
[amp] => 65
)
[NEW_SPEC] => stdClass Object
(
[volt] => 12 Volt
[series] => K1 Series
[amp] => 65
)
)
[SPECS] => stdClass Object
(
[OLD] => stdClass Object
(
[series] => K2
[volt] => 24
[amp] => 0
[rotation] => NO
[teeth] => 75
[splines] => 0
[kw] => 0.00
[o_d] => 1.625
)
[NEW] =>
[OLD_TYPE] => 90
)
)
I then pass specific groups to the processing page to remove letters and correct data mis-alignment issues. And data passed is shown below:
$CSF['spec_type'] = $FIXER_INPUTS->SPECS->OLD_TYPE;
$CSF['part_specs'] = $FIXER_INPUTS->SPECS->OLD;
$CSF['cctb_specs'] = $FIXER_INPUTS->CCTB->OLD_SPEC;
include("cctb_spec_fixer.php");/* processing page */
$FIXER_INPUTS->SPECS->OLD = $CSF['part_specs'];
$FIXER_INPUTS->CCTB->OLD_SPEC = $CSF['cctb_specs'];
And the array for the processing page is shown below:
(cctb_spec_fixer)initial)$CSF reads:
Array
(
[spec_type] => 90
[part_specs] => stdClass Object
(
[series] => K2
[volt] => 24
[amp] => 0
[rotation] => NO
[teeth] => 75
[splines] => 0
[kw] => 0.00
[o_d] => 1.625
)
[cctb_specs] => stdClass Object
(
[volt] => 12 Volt
[series] => K1 Series
[amp] => 65
)
)
After processing the returned result is shown below:
(cctb_spec_fixer)final) $CSF reads:
Array
(
[spec_type] => 90
[part_specs] => stdClass Object
(
[series] => K1
[volt] => 12
[amp] => 65
[rotation] => NO
[teeth] => 0
[splines] => 0
[kw] => 0
[o_d] => 1.625
)
[cctb_specs] => stdClass Object
(
[volt] => 12
[series] => K1
[amp] => 65
)
)
So far so good then when the page array is printed out for verification, I get this:
(AF:CCTB SPEC FIX)$FIXER_INPUTS reads:
stdClass Object
(
[CCTB] => stdClass Object
(
[OLD] => 90-15-6170
[NEW] => 90-15-6170
[OLD_SPEC] => stdClass Object
(
[volt] => 12
[series] => K1
[amp] => 65
)
[NEW_SPEC] => stdClass Object
(
[volt] => 12
[series] => K1
[amp] => 65
)
)
[SPECS] => stdClass Object
(
[OLD] => stdClass Object
(
[series] => K1
[volt] => 12
[amp] => 65
[rotation] => NO
[teeth] => 0
[splines] => 0
[kw] => 0
[o_d] => 1.625
)
[NEW] =>
[OLD_TYPE] => 90
)
)
As you can see both $FIXER_INPUTS->CCTB->OLD_SPEC and $FIXER_INPUTS->CCTB->NEW_SPEC are identical even though only one ($FIXER_INPUTS->CCTB->OLD_SPEC) was passed to the processing page.
Why did that happened? Does it have to do with my array structure? Does PHP lose its mind with similar array keys at similar levels?
UPDATE:
Since I used the following to get the data for both $FIXER_INPUTS->CCTB->OLD_SPEC and $FIXER_INPUTS->CCTB->NEW_SPEC:
$CCTB_SPEC_DATA = getChainSpecLIST($FIXER_INPUTS->CCTB->OLD, $dbc);
here is the code for 'getChainSpecLIST':
function getChainSpecLIST($CHAIN, $dbc){
// need both chain and type data
if($CHAIN){
global $SPEC_LIST_OLD;
$sql = "SELECT ".$SPEC_LIST_OLD[substr($CHAIN,0,2)]." FROM product_specs WHERE wil_no = \"".$CHAIN."\"";
$res = sql_query($sql, $dbc);
$num = mysqli_num_rows($res);
if($num > 0){
$row = mysqli_fetch_assoc($res);
$DATA_RETURN = (object) $row;
return (object)$DATA_RETURN;
}else{
return false;
}
}else{
return false;
}
}
$SPEC_LIST_OLD reads as follows:
$SPEC_LIST_OLD['90'] = 'volt, series, amp';
$SPEC_LIST_OLD['91'] = 'volt, rotation, series, kw, teeth';
$SPEC_LIST_OLD['92'] = 'volt, rotation, series, kw, splines';
$SPEC_LIST_OLD['94'] = 'volt, rotation, series, kw, splines';
Then I transfer the data to the page array ($FIXER_INPUTS):
$FIXER_INPUTS->CCTB->OLD_SPEC = $CCTB_SPEC_DATA;
$FIXER_INPUTS->CCTB->NEW_SPEC = $CCTB_SPEC_DATA;
Then I use the UNSET to destroy the $CCTB_SPEC_DATA data:
unset($CCTB_SPEC_DATA);
This is how I got the issue above. But I did some testing and I got a different result, which makes me wonder about UNSET function in the scheme of things. Does it truly remove the variable being unset or just clears values. To be truly UNSET means no trace of it to found throughout the page action after the variable is UNSET-ted.
This leads me to my findings:
I tried to test the building of data for $FIXER_INPUTS->CCTB->NEW_SPEC, to find at which degree when the above issue arises.
First, I tried setting the $FIXER_INPUTS->CCTB->NEW_SPEC to no child entries.
NO ISSUE AS SEEN ABOVE
Second, I tried setting individual child entries to blank data.
NO ISSUE AS SEEN ABOVE
Third, I tried setting individual child entries with data.
NO ISSUE AS SEEN ABOVE
Fourth, I re-wrote the code for filling the array as shown below:
$FIXER_INPUTS->CCTB->OLD_SPEC = getChainSpecLIST($FIXER_INPUTS->CCTB->OLD, $dbc);
$FIXER_INPUTS->CCTB->NEW_SPEC = getChainSpecLIST($FIXER_INPUTS->CCTB->OLD, $dbc);
I called the function(this application isn't built in OOP style with Classes) on each child separately and the end result was
NO ISSUE AS SEEN ABOVE
as seen in the array below:
(AF:CCTB SPEC FIX)$FIXER_INPUTS reads:
stdClass Object
(
[CCTB] => stdClass Object
(
[OLD] => 90-15-6170
[NEW] => 90-15-6170
[OLD_SPEC] => stdClass Object
(
[volt] => 12
[series] => K1
[amp] => 65
)
[NEW_SPEC] => stdClass Object
(
[volt] => 12 Volt
[series] => K1 Series
[amp] => 65
)
)
[SPECS] => stdClass Object
(
[OLD] => stdClass Object
(
[series] => K1
[volt] => 12
[amp] => 65
[rotation] => NO
[o_d] => 1.625
)
[NEW] =>
[OLD_TYPE] => 90
)
)
So now the real question is "why does UNSET truly not clear all traces of said variable from memory?" Some how calling a function once to get data (assigned to a single tmp-variable for passing along) and applying that data to two separate child arrays then UNSET-ting that tmp-variable truly doesn't UNSET the tmp-variable and all traces of said tmp-variable after UNSET is used.

how to read json data indexed object in php

Three simple lines of code:
$input = #file_get_contents("php://input");
$event_json = json_decode($input,true);
print_r($event_json);
I have used first 2 lines to convert json data into array in php, and third line prints the following array (a portion I show here):
Array
(
[created] => 1326853478
[livemode] =>
[id] => evt_00000000000000
[type] => charge.succeeded
[object] => event
[request] =>
[pending_webhooks] => 1
[api_version] => 2014-07-26
[data] => Array
(
[object] => Array
(
[id] => ch_00000000000000
[object] => charge
[amount] => 100
[amount_refunded] => 0
My question is: How do I echo amount (which is 100) or amount_refunded (which is 0)?
echo $event_json['data']['object']['amount'];
echo $event_json['data']['object']['amount_refunded'];

How can I loop through a stdObject's array without raising PHP notices/warnings?

I have the following stdObject obtained thru cURL / json_decode():
stdClass Object
(
[response] => stdClass Object
(
[status] => OK
[num_elements] => 1030
[start_element] => 0
[results] => stdClass Object
(
[publisher] => stdClass Object
(
[num_elements] => 1030
[results] => Array
(
[0] => stdClass Object
(
[id] => 1234
[weight] => 4444
[name] => Pub 1
[member_id] => 1
[state] => active
[code] =>
)
[1] => stdClass Object
(
[id] => 1235
[weight] => 4444
[name] => Pub 2
[member_id] => 2
[state] => active
[code] =>
)
)
)
)
[dbg_info] => stdClass Object
(
[instance] => instance1.server.com
[slave_hit] => 1
[db] => db1.server.com
[reads] => 3
[read_limit] => 100
[read_limit_seconds] => 60
[writes] => 0
[write_limit] => 60
[write_limit_seconds] => 60
[awesomesauce_cache_used] =>
[count_cache_used] =>
[warnings] => Array
(
)
[time] => 70.440053939819
[start_microtime] => 1380833763.4083
[version] => 1.14
[slave_lag] => 0
[member_last_modified_age] => 2083072
)
)
)
I'm looping through it in order to obtain each result's ID:
foreach ($result->response->results->publisher->results as $object) {
$publishers .= $object->id.",";
}
And although the code is working fine, PHP is rising the following notices/warnings:
PHP Notice: Trying to get property of non-object in /var/www/vhosts/domain.net/script.php on line 1
PHP Notice: Trying to get property of non-object in /var/www/vhosts/domain.net/script.php on line 1
PHP Warning: Invalid argument supplied for foreach() in /var/www/vhosts/domain.net/script.php on line 1
Any ideas? Thanks in advance!
Is it possible you have set the second parameter $assoc in json_decode() to true? Like this:
$result = json_decode($json_from_curl, true);
Thereby, getting back $result with all stdClass Objects converted to associative array.
EDIT:
If the $result you are getting is indeed an associative array, then we should treat it as such. Try replacing your foreach with this:
foreach ($result['response']['results']['publisher']['results'] as $arr) {
$publishers .= $arr['id'] . ",";
}
EDIT2:
From my own testing based on your code, everything should be working correctly. The notices/warnings should not occur. Perhaps some other code not shown in your question is causing it.

How to display php array as HTML?

I am working with the Yahoo BOSS API to build an image search for motherpipe.co.uk.
I have managed to create a valid request for image listings and have received the response. My problem is that I don't understand how I can use the different elements of that response to build my nice-looking page of image listings.
Ideally I want to loop through the array and display a thumbnail and link for each item in the list but somehow I can't abstract the relevant bits from the $results.
The (example) output from the query with two listings is in this $results:
stdClass Object ( [bossresponse] => stdClass Object ( [responsecode] => 200 [images] => stdClass Object ( [start] => 0 [count] => 2 [totalresults] => 107000 [results] => Array ( [0] => stdClass Object ( [clickurl] => htt://library.thinkquest.org/07aug/01105/Sweden/stockholm.jpg [size] => 191.8KB [format] => jpeg [height] => 586 [refererclickurl] => htt://library.thinkquest.org/07aug/01105/Sweden/sweden_home.html [refererurl] => htt://library.thinkquest.org/07aug/01105/Sweden/sweden_home.html [title] => Stockholm is a beautiful city with Lake Mälaren on it’s WestSide ... [url] => http://library.thinkquest.org/07aug/01105/Sweden/stockholm.jpg [width] => 793 [thumbnailheight] => 118 [thumbnailurl] => htt://ts4.mm.bing.net/th?id=H.4970051277687231&pid=15.1&H=118&W=160 [thumbnailwidth] => 160 ) [1] => stdClass Object ( [clickurl] => http://summerventures.files.wordpress.com/2012/01/429c_stockholm_ch.jpg [size] => 2.3MB [format] => jpeg [height] => 1272 [refererclickurl] => htt://summerventures.wordpress.com/tag/stockholm/ [refererurl] => htt://summerventures.wordpress.com/tag/stockholm/ [title] => stockholm | Summer adventures [url] => htt://summerventures.files.wordpress.com/2012/01/429c_stockholm_ch.jpg [width] => 1800 [thumbnailheight] => 113 [thumbnailurl] => htt://ts2.mm.bing.net/th?id=H.4581116279128437&pid=15.1&H=113&W=160 [thumbnailwidth] => 160 ) ) ) ) )
Question:
What approach can I use to simply display a thumbnail picture with a link from each of the listings in this array, using the info in [thumbnailurl] and [refererclickurl] kind of like
<div> <<a href="[refererclickurl]"><img src="[thumbnailurl]" alt="alt text" /> </div>
I think I need a for each approach but after having tried for three days I can't make it work.
Any help much appreciated.
Try the following. The quoted object in your question is in the $o variable.
$html = '';
$images = $o->bossresponse->images->results;
foreach ($images as $image) {
$html .= "<a href='{$image->refererclickurl}'><img src='{$image->thumbnailurl}'></a>";
}
echo $html;
Note that if $obj->prop equals 'foo' then "{$obj->prop} is not bar" would equal 'foo is not bar'.
Its also good to note that the html would be better if you also output the image dimensions and may change depending on what html spec you're implementing.

Store into a database table as row the query result

query result
Array
(
[0] => stdClass Object
(
[ingredientID] => 2
[code] => Bf
[description] => 1st Class Flour
[volume] => 8268
[price] => 750
[amount_gram] => 0.02980
[status] => Inactive
[uom_id] => 1
[flour] => Yes
)
[1] => stdClass Object
(
[ingredientID] => 3
[code] => Sf
[description] => 3rd Class Flour
[volume] => 18490
[price] => 635
[amount_gram] => 0.02540
[status] => Inactive
[uom_id] => 5
[flour] => Yes
)
..........
I want to store this results into another table as row inventory.
the table will look like this:
ID inventory
1 (the result)
2 (another result)
And after I will query it back again so that I can display the result.
here's what I have done lately.
store:
//getting the result
$inv = $this->db->get_from('table','id'=>'1')->row();
<input type="hidden" name="inventory" value="<?php print_r($inv)?>">
//storing in the new table
$this->db->insert('table2',array('inventory'=>$this->input->post('inventory')));
getting:
$inventory = $this->db->get_where('table2',array('ID'=>'1'))->row_array();
//result
array
(
[ID] => 1
[inventory] =>
array
(
[0] => stdClass Object
(
[ingredientID] => 2
...... and so on
I want to display everything in the array['inventory'] which is an array of objects.
I've done this
foreach($arr['inventory'] as $invent):
echo $invent['ingredientID'];
but there's an error in the foreach part.
error: Invalid argument supplied for foreach()
What should i do?
endforeach;
assuming:
$results = $this->db->get_where('table2',array('ID'=>'1'))->row_array();
you should use this to print it
foreach($results['inventory'] as $inventory)
{
print_r($inventory->ingredientID);
}

Categories