I have this array
{
"findCompletedItemsResponse": [
{
"ack": [
"Success"
],
"version": [
"1.13.0"
],
"timestamp": [
"2018-12-16T18:27:26.221Z"
],
"searchResult": [
{
"#count": "3",
"item": [
{
"itemId": [
"263933812890"
],... continue
I am using this code to get "itemId"
for( $i = 0; $i<5; $i++ ) {
echo $itemid = $data2['findCompletedItemsResponse']['searchResult']['item'][$i]['itemId'];
}
But I am not able to get "itemId". How can I get itemId from this array.
It's also displaying a notice "Undefined index: searchResult"
Under findCompletedItemsResponse key You have simple array with numeric indexes, same for searchResult, so assuming that they both only contains one element:
$data2['findCompletedItemsResponse'][0]['searchResult'][0]['item'][$i]['itemId'];
Looks like this is JSON response, you probably need to json decode it:
json_decode($data2, true);
see if this helps
Related
i am using Codeigniter and inside a function i call a controller to return data which I want to use in an foreach to built an insert statement.
The JSON returned looks like this:
{
"Data": [
{
"id": "743",
"day": "1",
"day_type": "Party",
"day_dresscode": "Black",
"day_p_id": "1",
"description": "Test desc",
"name": "test"
},
{
"id": "743",
"day": "2",
"day_type": "Party",
"day_dresscode": "White",
"day_p_id": "1",
"description": "Test desc 1",
"name": "test 2"
}
]
}
my php looks like below:
// This where i got my JSON
$datavar = $this->model->get_json_data($id,$code);
// This is to decode the JSON
$datavar_decode['Data'] = json_decode($datavar,true);
Below is my foreach:
$i = 1;
foreach($datavar_decode['Data'] as $data):
$data_insert = array(
'val1' => $data['val1'],
'val2' => $data['val2'],
'val3' => $data['val3'],
'val4' => $data['val4']
);
$this->db->insert('mssqltable', $data_insert);
$i++;
endforeach;
It gives me below error:
Invalid argument supplied for foreach()
Don't use json.
You're problem is that you are returning an array of objects, and that's the behavior you get from $query->result() or if you use $query->row() then you get an object representing a single row.
In order to return an array, use $query->row_array() for a single row, or for multiple records returned you would use $query->result_array().
See all the details: https://www.codeigniter.com/user_guide/database/results.html
You can try this code to test that your $datavar_decode['Data'] doesn't allocate an empty array when you've got nothing to begin with anyway.
if (is_array($datavar_decode['Data']) || is_object($datavar_decode['Data']))
{
foreach ($datavar_decode['Data'] as $data)
{
...
}
}
I am trying to create mongoDB subdocuments record inside PHP code,
{
"_id": "",
"ref": [
{
"crm_base_contact_id": "1653",
"crm_imported_files_id": "906"
}
],
"data": [
{
"First_name": "Annalee",
"Last_name": "Graleski",
},
{
"First_name": "Henry",
"Last_name": "Smith",
}
],
}
How to create two arrays inside "data" subdocuments in php code .
Please provide me any idea to insert this in mongoDB using PHP code,
You can add additional fields with the $set operator when updating a document:
db.collection.update({},{"$set": { "history": "value" }})
If you want that field to be an array or sub-document then it is just the same:
db.collection.update({},{"$set": { "history": ["a","b","c"] }})
If you are struggling with converting the JSON syntax into php code, then here is something that will help you in the future:
$result = '{"$set": { "history": ["a","b","c"] }}';
echo var_dump( json_decode( $result ) );
$test = array( '$set' => array('history' => array( 'a', 'b', 'c') ) );
echo json_encode( $test ) ."\n"
So the first line just decodes the json and will dump what it should look like. If you are not sure, then json_encode your php array declaration to see that you have it right.
I have the following php script in which I attempt to write a JSON file with contents of an array:
$name = "TEST";
$type = "TEST";
$price = "TEST";
$upload_info = array();
$upload_info = array('name'=>$name,'type'=>$type,'price'=>$price);
$temp_array = json_decode(file_get_contents('upload.json'));
array_push($temp_array, $upload_info);
file_put_contents('items.json', json_encode($temp_array));
$json = json_encode($upload_info);
$file = "items.json";
file_put_contents($file, $json);
This will add the following line to items.json:
{"name":"TEST","type":"TEST","price":"TEST"}
It also gives this error:
PHP Warning: array_push() expects parameter 1 to be array, null given in /var/www/html/list/add.php on line 13
What I would ideally like to do is to have my script add to specific parts of a json file that looks something like this:
{
"GC": [
{ "Name":"Thing" , "Price":"??" , "Link":"www.google.com" },
{ "Name":"Thing" , "Price":"??" , "Link":"www.google.com" }
]
"0": [
]
"10": [
]
"20": [
]
"30": [
]
"40": [
]
"50": [
]
"60": [
]
"70": [
]
"80": [
]
"90": [
]
"100": [
]
"200": [
]
"300": [
]
"400": [
]
"500": [
]
}
Each one of those arrays would hold items (similar to the GC array) after they have been added with add.php. Is this possible? If so, how would I do it?
Is it possible to select specific parts of the json where I would want to add the entries? Is there an easier way to do this? I am just looking for a solution to storing my data without using a database.
you need to use second parameter true, of json_decode() to get array result, like:
$temp_array = json_decode(file_get_contents('upload.json'), true);
//get the needed values from $temp_array
//create a new array out of it
file_put_contents('items.json', json_encode($your_final_array));
$array = array(
$this->_order->revenue_seller($value,$from,$to),
$this->_order->refund_seller($value,$from,$to),
$this->_order->customers_seller($value,$from,$to),
$this->_order->sales_seller($value,$from,$to)
);
$this->response($array,200);
I want the data to be be displayed as a single array, but as we can see, the output is begin displayed as array of arrays:
[
[
{
"min_amount":"2.00",
"max_amount":"2.00",
"avg_amount":"2.000000",
"total_revenue":"2.00"
}
],
[
{
"total_refund_amount":"1.00"
}
],
[
{
"created_at":"2013-03-24 15:04:35"
}
],
[
{
"quantity":"1"
}
]
]
How can I make the data appear as one single array?
Like this:
[
{
"day":"2013-03-19",
"min_amount":"0.00",
"max_amount":"0.00",
"avg_amount":"0.000000",
"total_revenue":"0.00",
"quantity" : "1",
"total_refunded_amount":null,
"created_at":"2013-03-24 15:04:35"
}]
If you are getting array return by all four function than use array_merge as,
$array = array_merge($this->_order->revenue_seller($value,$from,$to),
$this->_order-refund_seller($value,$from,$to),
$this->_order->customers_seller($value,$from,$to),
$this-_order->sales_seller($value,$from,$to));
$this->response($array,200);
DEMO.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 11 months ago.
All,
I have the following JSON Data. I need help writing a function in PHP which takes a categoryid and returns all URLs belonging to it in an array.
Something like this::
<?php
function returnCategoryURLs(catId)
{
//Parse the JSON data here..
return URLArray;
}
?>
{
"jsondata": [
{
"categoryid": [
20
],
"url": "www.google.com"
},
{
"categoryid": [
20
],
"url": "www.yahoo.com"
},
{
"categoryid": [
30
],
"url": "www.cnn.com"
},
{
"categoryid": [
30
],
"url": "www.time.com"
},
{
"categoryid": [
5,
6,
30
],
"url": "www.microsoft.com"
},
{
"categoryid": [
30
],
"url": "www.freshmeat.com"
}
]
}
Thanks
What about something like this :
You first use json_decode, which is php's built-in function to decode JSON data :
$json = '{
...
}';
$data = json_decode($json);
Here, you can seen what PHP kind of data (i.e. objects, arrays, ...) the decoding of the JSON string gave you, using, for example :
var_dump($data);
And, then, you loop over the data items, searching in each element's categoryid if the $catId you are searching for is in the list -- in_array helps doing that :
$catId = 30;
$urls = array();
foreach ($data->jsondata as $d) {
if (in_array($catId, $d->categoryid)) {
$urls[] = $d->url;
}
}
And, each time you find a match, add the url to an array...
Which means that, at the end of the loop, you have the list of URLs :
var_dump($urls);
Gives you, in this example :
array
0 => string 'www.cnn.com' (length=11)
1 => string 'www.time.com' (length=12)
2 => string 'www.microsoft.com' (length=17)
3 => string 'www.freshmeat.com' (length=17)
Up to you to build from this -- there shouldn't be much left to do ;-)
Try the built-in json_decode function.