I have the following json file with products details:
"products": [
{
"sku": 123,
"name": "iphone 7",
"categoryPath": [
{
"id": "abcat0800000",
"name": "Cell Phones"
},
{
"id": "pcmcat209400050001",
"name": "All Cell Phones with Plans"
}
],
}
]
I would like only to store the last value (ID and NAME) of the categoryPath Array:
"id": "pcmcat209400050001",
"name": "All Cell Phones with Plans"
My current code takes the json file, decode the json and insert in products table the information.
$json = File::get("/json/cell-0.json");
$data = json_decode($json);
$array1 = (array)$data;
//table products
foreach ($array1['products'] as $obj) {
DB::table('products')->insert(array(
'productSku' => ((isset($obj->sku) ? $obj->sku : 1)),
'productName' => ((isset($obj->name) ? $obj->name : null)),
'categoryId' => end($obj->categoryPath->id),
'categoryName' => end($obj->categoryPath->name)
));
Taking into consideration that array->categoryPath have multiple fields I would like to use a function (eg: end()) in order to take id and name only of the last values.
Using end($obj->categoryPath->id) I receive the following error ->
Attempt to modify property of non-object
Is this the best way to retrieve the last value of a multidimensional array?
You could use end() probably but your accessors would have to be outside the end() call (untested):
foreach ($array1['products'] as $obj) {
DB::table('products')->insert(array(
'productSku' => ((isset($obj->sku) ? $obj->sku : 1)),
'productName' => ((isset($obj->name) ? $obj->name : null)),
'categoryId' => end($obj->categoryPath)->id,
'categoryName' => end($obj->categoryPath)->name
));
The way you're getting the last element is incorrect, here is the refactored code. I also eliminated the need to cast data as an array as well.
$json = File::get("/json/cell-0.json");
$data = json_decode($json, true);
//table products
foreach ($data['products'] as $product) {
$lastCategory = isset($product['categoryPath']) && $size = sizeof($product['categoryPath']) ? $product['categoryPath'][$size-1] : array('id' => null, 'name' => null);
DB::table('products')->insert(
array(
'productSku' => isset($product['sku']) ? $product['sku'] : 1,
'productName' => isset($product['name']) ? $product['name'] : null,
'categoryId' => lastCategory['id'],
'categoryName' => lastCategory['name']
)
);
}
Related
I am rather new to PHP so I don't know how to work with these datasets. I make a MySQL select and get back an object like this:
{
"membername": "NAME",
"bookingdate": "2020-02-03",
"categoryid": 1,
"dailyworkhourssum": "7.70"
},
{
"membername": "NAME",
"bookingdate": "2020-02-03",
"categoryid": 3,
"dailyworkhourssum": "1.2"
},
{
"membername": "NAME",
"bookingdate": "2020-02-05",
"categoryid": 3,
"dailyworkhourssum": "7.70"
},
I want to iterate through this and in the end it should look like this:
{
"membername": "NAME",
"bookingdate": "2020-02-03",
"categoryid1": true,
"categorid3": true,
"dailyworkhourssum1": "7.70",
"dailyworkhourssum3": "1.2"
},
{
"membername": "NAME",
"bookingdate": "2020-02-05",
"categoryid": 3,
"dailyworkhourssum": "7.70"
},
What this does is that it merges tow fields together (if they have the same bookingdate )into one so that I can display it in a table without reoccurring dates.
My problem:
I don't know what this type of data is called.
I don't know how to create something like this.
I can add fields to this type of data with $data->newField = example so I think that this is an object.
In JS it's called an object, but in PHP you will use an associative array instead.
In your case, I think, you have an array of associative arrays. It looks like this:
$books = [
[
"membername" => "NAME",
"bookingdate" => "2020-02-03",
"categoryid" => 1,
"dailyworkhourssum" => "7.70"
],
[
"membername" => "NAME",
"bookingdate" => "2020-02-03",
"categoryid" => 3,
"dailyworkhourssum" => "1.2"
],
[
"membername" => "NAME",
"bookingdate" => "2020-02-05",
"categoryid" => 3,
"dailyworkhourssum" => "7.70"
]
];
If you wanna merge an arrays with the same "bookingdate" then I recommend you to loop through this array and add its elements to another associative array with bookingdates as keys, and check, in case if there is such key already, then merge the arrays, like this:
$merged = [];
foreach ($books as $book) {
$date = $book['bookingdate'];
if (isset($merged[$date])) {
$merged[$date] = $merged[$date] + $book;
} else {
$merged[$date] = $book;
}
}
I think that it is not a valid code (no time, sorry), but I hope, you cautch the idea.
If you want a 'list' instead of an associative array, than you can do this:
$mergedList = array_values($merged);
Thus you will rid of string keys.
If I understood correctly, you obtain a table with 4 columns an a variable number of rows and you want to transform it to a table with a variable number of columns. For that, using a data structure where every item is different from the previous one can make everything harder than it needs. I'd suggest you use a fixed structure:
// I'm assuming you have a PHP array as starting point
$input = [
[
'membername' => 'NAME',
'bookingdate' => '2020-02-03',
'categoryid' => 1,
'dailyworkhourssum' => '7.70',
],
[
'membername' => 'NAME',
'bookingdate' => '2020-02-03',
'categoryid' => 3,
'dailyworkhourssum' => '1.2',
],
[
'membername' => 'NAME',
'bookingdate' => '2020-02-05',
'categoryid' => 3,
'dailyworkhourssum' => '7.70',
],
];
$output = [];
foreach ($input as $data) {
// We'll group by booking date
if (!isset($output[$data['bookingdate']])) {
$output[$data['bookingdate']] = [
'membername' => $data['membername'],
'bookingdate' => $data['bookingdate'],
'categoryid' => $data['categoryid'],
'dailyworkhourssum' => [],
];
}
// A single date may have several daily work hours
$output[$data['bookingdate']]['dailyworkhourssum'][] = $data['dailyworkhourssum'];
}
// We discard array keys (we only needed them to group)
echo json_encode(array_values($output));
[{
"membername": "NAME",
"bookingdate": "2020-02-03",
"categoryid": 1,
"dailyworkhourssum": ["7.70", "1.2"]
}, {
"membername": "NAME",
"bookingdate": "2020-02-05",
"categoryid": 3,
"dailyworkhourssum": ["7.70"]
}]
Wherever you consume this JSON you just need to loop the dailyworkhourssum array. You may also want to loop the entire structure before printing the table and keep a counter in order to determine the maximum number of columns so you can draw empty cells where needed (tables are rectangular).
i am using laravel to make an API that queries a database and show a simple output. Now my query goes like this:
$name = new TableName() ; //An object which is storing tablename
$result=DB::table($name->getMyTableName()) // Getting the tablename
->select()
->where('id','=',1)->orWhere('id','=',2)
->get();
Now this will fetch two records with id 1 and 2 and when I pass them through my controller, the json out put comes like this:
[
{
"id": "1",
"question": "This is",
"question_main": "sample1",
"option_1": "opt_1",
"option_2": "opt_1",
"option_3": "opt_1",
"option_4": "opt_1",
},
{
"id": "2",
"question": "This is",
"question_main": "sample2",
"option_1": "opt_1",
"option_2": "opt_2",
"option_3": "opt_3",
"option_4": "opt_4",
}
]
Now what I want is instead of option, i have something like this:
option :
{
1=>"opt_1",
2=>"opt_2",
3=>"opt_3",
4=>"opt_4",
}
Now what i tried is this:
foreach ($result as $results)
{
$final_result= array_add($final_result, 'id', $results->id);
$final_result= array_add($final_result, 'question', $results->question);
$final_result= array_add($final_result, 'question_main', $results->question_main);
$final_result= array_add($final_result, 'options', array(
[
1 => $results->option_1,
2 => $results->option_2,
3 => $results->option_3,
4 => $results->option_4,
]
));
}
return json_encode($final_result,JSON_PRETTY_PRINT);
This pretty much solves the problem however as you might have figured out key-value pair can have unique key so it will just store id=2 (the latter). I am pretty much confused on what to follow. I tried Multi dimensional array but i think i couldn't get the concept right on that.Any suggestions, thanks in advance
You can do the following using the key in your foreach:
$final_result = [];
foreach ($result as $key => $results)
{
$final_result[$key] = [
'id' => $results->id,
'question' => $results->question,
'question_main' => $results->question_main,
'options' => [
1 => $results->option_1,
2 => $results->option_2,
3 => $results->option_3,
4 => $results->option_4
]
];
}
return json_encode($final_result,JSON_PRETTY_PRINT);
This would do the desired.
$arrayToBePrinted = [];
foreach ($result as $results)
{
$final_result= array_add($final_result, 'id', $results->id);
$final_result= array_add($final_result, 'question', $results->question);
$final_result= array_add($final_result, 'question_main', $results->question_main);
$final_result= array_add($final_result, 'options', array(
[
1 => $results->option_1,
2 => $results->option_2,
3 => $results->option_3,
4 => $results->option_4,
]
));
$arrayToBePrinted[] = $final_result;
}
return json_encode($arrayToBePrinted,JSON_PRETTY_PRINT);
I'm trying to use a mysql stored procedure to return a JSON string which can be turned into an associative array using PHP's json_decode function. I have a valid JSON string being returned from the s.p but it isn't in the correct form or json_decode($skusJson, true); doesn't like it for some reason. The PHP code for getting the s.p results and doing the json_decode is:
$partsQuery = $this->Orders->callSP('part_skus_dropdown1');
$skusJson = $partsQuery->fetchAll('assoc');
$partsQuery->closeCursor();
$skus = json_decode($skusJson[0]['json'], true);
The mysql s.p is:
CREATE DEFINER=`root`#`localhost` PROCEDURE `part_skus_dropdown1`()
BEGIN
SELECT
CONCAT('[',
GROUP_CONCAT(
CONCAT('{"id":',id),
CONCAT(',"sku":"',sku,'"}')
),
']')
AS json FROM parts where id < 25;
END
The resulting JSON string is:
[
{
"id" : 1,
"sku" : "1"
},
{
"id" : 3,
"sku" : "3"
},
{
"id" : 6,
"sku" : "6"
},
{
"id" : 7,
"sku" : "7"
},
{
"id" : 9,
"sku" : "9"
}
]
(The sku's will not always match the id's and are stored as strings rather than ints). The results of the json_decoding are:
[
(int) 0 => [
'id' => (int) 1,
'sku' => '1'
],
(int) 1 => [
'id' => (int) 3,
'sku' => '3'
],
(int) 2 => [
'id' => (int) 6,
'sku' => '6'
],
(int) 3 => [
'id' => (int) 7,
'sku' => '7'
]
]
Is there anyway that I can get the resulting array to be in the form of [ id => sku ]: (alt. sku format shown)
[
(int) 1 => '1',
(int) 3 => '3',
(int) 58 => '3-BOX100'
]
I'm stuck... I've been following the http://php.net/json_decode documentation and using http://jsonlint.com/ to check the results of the s.p but think its time to ask for help... thanks in advance :)
Change your procedure so that it returns a single JSON object, not an array, and then uses id as the key and sku as the value in each element
CREATE DEFINER=`root`#`localhost` PROCEDURE `part_skus_dropdown1`()
BEGIN
SELECT
CONCAT('{',
GROUP_CONCAT(
CONCAT('"',id, '": "',sku,'"')
),
'}')
AS json FROM parts where id < 25;
END
DEMO
Note that the keys of a JSON object are always strings, they can't be integers. But both PHP and Javascript will automatically convert between integers and strings when accecssing the array.
your problem is that you need to turn the rows into an associative array keyed by the id. this will do that
function rowsToAssoc ($rows) {
$assoc = Array();
for ($rows as $r) {
$assoc[$r['id']] = $r['sku'];
}
return $assoc;
}
then you can json_encode/decode as you wish.
if you are using PDO for your MySQL calls, check out this answer to avoid looping completely
i have googled for solution to my problem but nun helped me.
here i have three tables items, feeds and images. each item has one feed and one or more images.
i have 3 functions. one is to return records from items table the second one receives feeds_id (foreign key in items table) then return records from feeds table. the third function is to return all images related to items_id.
those functions are :
* To get all items in database:
function get_items(){
return $query = Database::getInstance('db')
->table('items')
->columns(
'id',
'items.rowid',
'items.feed_id as feed_id',
'title' )
->findAll();
}
* To get feed data from feeds table :
function get_feeds($id){
return $query = Database::getInstance('db')
->table('feeds')
->eq('id',$id)
->findAll();
}
* To get image data from images table :
function get_images($id){
return $query = Database::getInstance('db')
->table('images')
->columns('items_id','src as image_url',
'title as image_title',
'alt')
->eq('items_id',$id)
->findAll();
}
Then i have the following code to call those function and display the result in jsonformat:
$response['items'] = array();
$response['feeds'] = array();
$response['images'] = array();
foreach ($items = get_items() as $item) {
$response['items'][] = array(
'id' => (int)$item['rowid'],
'feed_id' => (int)$item['feed_id'],
'title' => $item['title'],
);
foreach ($feeds = get_feeds((int)$item['feed_id']) as $feed) {
$response['feeds'][] = array(
'title' => $feed['title'],
'logo_url' => $feed['logo_url'],
'site_url' => $feed['site_url'],
);
}
foreach ($images = get_images($item['id']) as $image) {
$response['images'][] = array(
'id' => $image['items_id'],
'url' => $image['image_url'],
'thumb' => $_SERVER['SERVER_NAME'] . /myServer/images/thumbs/'. 'thumb_'.basename($image['image_url']),
'title' => $image['image_title'],
'alt' => $image['alt']
);
}
}
echo json_encode($response, JSON_PRETTY_PRINT);
so, my expectation is to get json output like:
"items": [
{
"id": ,
"feed_id":
"title":
"feeds": [
{
"title": ,
"logo_url": ,
"site_url": "
}
]
"images": [
{
"id": ,
"url": ",
"thumb":
"title": "",
"alt": ""
},
{
....
}
]
}]
i mean each item array should include nested arrays of its related data coming from get_feeds and get_images functions.
instead of that, i get response like :
//here i select two items from my db
"items": [
{ //first_item
"id": ,
"feed_id":
"title":
},
{ //second_item
"id": ,
"feed_id":
"title":
}
],
"feeds": [
{ // feed data for first item
"title": ,
"logo_url": ,
"site_url": "
},
{ // feed data for second item
"title": ,
"logo_url": ,
"site_url": "
}
],
"images": [
{ // image data for first item
"id": ,
"url": ",
"thumb":
"title": "",
"alt": ""
},
{ // other images data
....
}
]
}]
as you see i am getting output without keeping relation between items, feeds and images, all of them are shown independently.
my queries are fine but i am suspecting error in my foreach statements.
i could fix this issue by joining those tree tables in one query, but i don't want to do that because i need to do validation and other operations to output comes from each table.
i appreciate your help
i found the solution. it is very easy :)
it is just like:
$response['items'][] = array(
'id' => (int)$item['rowid'],
'feed_id' => (int)$item['feed_id'],
'title' => $item['title'],
'feeds' => array(
)
'images' => array(
)
);
CakePHP API returns result like this:
{
"status": "OK",
"themes": [
{
"Theme": {
"id": "20",
"user_id": "50",
"name": "dwdwdw",
"language_code_from": "cz",
"language_code_to": "en",
"type": "CUSTOM",
"created": "2014-10-19 15:36:05",
"count_of_cards": 0
}
}
]
}
I would like to ask, how can in remove nested Theme object to get result like this?:
{
"status": "OK",
"themes": [
{
"id": "20",
"user_id": "50",
"name": "dwdwdw",
"language_code_from": "cz",
"language_code_to": "en",
"type": "CUSTOM",
"created": "2014-10-19 15:36:05",
"count_of_cards": 0
}
]
}
Here is my CakePHP code:
$this->Theme->recursive = -1;
// GET USER ID
$themeData['user_id'] = $isSessionValid;
// GET ALL THEMES RELATED TO USER
$foundThemes = $this->Theme->find('all', array(
'conditions' => array(
'Theme.user_id' => $themeData['user_id'])
)
);
$themes = array();
// FOREACH THEMES AND GET COUNT FOR CARDS FOR EACH THEME
foreach($foundThemes as $foundTheme) {
// GET COUNT OF QUESTIONS FOR ACTUAL THEME
$countOfCards = $this->Theme->Card->find('count', array(
'conditions' => array(
'Card.theme_id' => $foundTheme['Theme']['id'])
)
);
// APPEND TO ACTUAL ARRAY
$foundTheme['Theme']['count_of_cards'] = $countOfCards;
array_push($themes,$foundTheme);
}
// SET SUCCESS RESPOSNSE
$this->set(array(
'status' => 'OK',
'themes' => $themes,
'_serialize' => array(
'status',
'themes',
)
));
Many thanks for any advice.
You can manipulate CakePHP's array formats using its built in Hash utility: http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash
What I would do would be to flatten the results:
$results = Hash::flatten($results);
Your data array will end up as a single dimensional array looking like this:
$results = array(
'status' => 'OK'
'themes.0.Theme.id' => 20,
...
'themes.1.Theme.id' => 21,
...
);
You can then use string replace to remove "Theme" from your keys:
$keys = array_keys($results);
$keys = str_replace('Theme.', '', $keys);
Then you can use Hash::expand to get your original array, now formatted how you want:
$results = Hash::expand(array_combine($keys, array_values($results)));
I dont think CakePHP supports this. if you want to do this with an easy way check the Set Utility.
http://book.cakephp.org/2.0/en/core-utility-libraries/set.html