Multidimensional array push manually php codeigniter - php

Array(
[0] => stdClass Object
(
[id] => 1
[user_id] => 1
[following_id] => 2
[type] => user
[created_on] => 2014-03-01 04:43:57
)
[1] => stdClass Object
(
[id] => 15
[user_id] => 1
[following_id] => 4
[type] => user
[created_on] => 2014-05-27 11:55:17
)
)
Hi friends,
I have an array of products that I need to create manually next stdclass Object.These array are generated by pushing value.
I'm trying to solve this for more than an hour now, but I don't get it to work. I know it should be easy...but anyway - I don't get it :D
I have working for an hour to create manually next stdclass Object. I want like this.
How do i create this.
Can anyone help with this?
Thanks in Advance,
J
Array(
[0] => stdClass Object
(
[id] => 1
[user_id] => 1
[following_id] => 2
[type] => user
[created_on] => 2014-03-01 04:43:57
)
[1] => stdClass Object
(
[id] => 15
[user_id] => 1
[following_id] => 4
[type] => user
[created_on] => 2014-05-27 11:55:17
)
[2] => stdClass Object
(
[id] => 16
[user_id] => 1
[following_id] => 5
[type] => user
[created_on] => 2014-05-27 11:55:17
)
)

One way of doing this is to use a function like so :
function create($id, $user_id, $following_id, $type, $created_on){
$obj = new stdClass;
$obj->id = $id;
$obj->user_id = $user_id;
$obj->following_id = $following_id;
$obj->type = $type;
$obj->created_on = $created_on;
return $obj;
}
And assuming your array is $array, you add items to it like this:
$array[] = create(16, 1, 5, 'user', '2014-05-27 11:55:17');
Hope this helps

Related

Push array object into another array object in php

I'm getting an error while pushing one object into another object. But the 2nd object is an array and inside an array there is an object. How can I fix this cause I want to add that into my object
My object just like this
I want to add the the Object2 into Object1
Objet1
stdClass Object
(
[id_laporan_pemeriksa] => 5
[no_pkpt] => SNE
[tgl_pkpt] => 2010
[no_penugasan] => ST-4000/PW25/2/2017
[tgl_penugasan] => 2017-08-09
[judul_laporan] => Masukkan Kode disini
[no_laporan] => LBINA-9000/PW25/2/2017
[tgl_laporan] => 2017-08-01
[tahun_anggaran_penugasan] => 2009
[nilai_anggaran_penugasan] => 10000000
[realisasi_anggaran_penugasan] => 100000000
[jenis_anggaran_penugasan] => Utang
[sumber_laporan] => Inspektorat Maluku
[nama_sumber_penugasan] => PKPT
[nama_ketua_tim] => Abdul Rofiek, Ak.
[nama_pengendali_teknis] => Alfian Massagony, S.E.
[nama_unit_penugasan] => Irban Wil. I
[nama_penugasan] => Penjaminan
[nama_sub_penugasan] => Audit
[id_s_sub_penugasan] => 010105
[nama_s_sub_penugasan] => Audit atas hal-hal lain di bidang kepegawaian.
)
Object2
stdClass Object
(
[id] => 3
[data_sebab] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_rekomendasi] => Array
(
[0] => stdClass Object
(
[id] => 4
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 9
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
[1] => stdClass Object
(
[id] => 5
[id_rekomendasi] =>
[data_tindak_lanjut] => Array
(
[0] => stdClass Object
(
[id] => 10
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
[1] => stdClass Object
(
[id] => 11
[id_tindak_lanjut] =>
[tgl_tindak_lanjut] => 0000-00-00
)
)
)
)
)
)
)
I have tried
$Object1['data']->$Object2;
But i got an error
Cannot use object of type stdClass as array
The syntax of adding $Object2 as a property of $Object1 is:
$Object1->Object2 = $Object2;
Or:
$Object1->{'Object2'} = $Object2;
It should be:
$Object1->data = $Object2; // it will create data element with obj2 as value
As the objects are objects and not arrays, using:
$Object1['data']->$Object2;
wont work. However doing the following will work:
$Object1->data = $Object2;

PHP json_encode() specific key of an array

I am using PHP 5.5.12.
I have an array like:
Array
(
[0] => Array
(
[user_id] => 3
[medicine_id] => 1
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_breakfast
[time] => 07:00:00
)
[1] => stdClass Object
(
[event_type] => after_breakfast
[time] => 07:30:00
)
)
)
[1] => Array
(
[user_id] => 3
[medicine_id] => 2
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_lunch
[time] => 13:00:00
)
[1] => stdClass Object
(
[event_type] => after_lunch
[time] => 14:00:00
)
)
)
[2] => Array
(
[user_id] => 3
[medicine_id] => 3
[time] => Array
(
[0] => stdClass Object
(
[event_type] => before_dinner
[time] => 20:00:00
)
[1] => stdClass Object
(
[event_type] => after_lunch
[time] => 21:00:00
)
)
)
)
I want to json_encode() the field time of each root level.
I tried using:
foreach ($user_medicine_times as $user_medicine_key => $user_medicine_value) {
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}
and:
foreach ($user_medicine_times as $user_medicine_key => &$user_medicine_value) {
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}
But using print_r($user_medicine_value), it returns the same array.
I want the result to be as follows:
Array
(
[0] => Array
(
[user_id] => 3
[medicine_id] => 1
[time] => "[{"event_type":"before_breakfast","time":"07:00:00"},{"event_type":"after_breakfast","time":"07:30:00"}]"
)
[1] => Array
(
[user_id] => 3
[medicine_id] => 2
[time] => "[{"event_type":"before_lunch","time":"13:00:00"},{"event_type":"after_lunch","time":"17:00:00"}]"
)
[2] => Array
(
[user_id] => 3
[medicine_id] => 3
[time] => "[{"event_type":"before_dinner","time":"20:00:00"},{"event_type":"after_lunch","time":"17:00:00"}]"
)
)
How can I achieve this result?
I have read your question earlier and prepared the answer but you removed it before i paste the answer. Anyways here is the solution
function outer(&$val, $key) {
$val['time'] = json_encode($val['time']);
}
array_walk($your_array, 'outer');
print_r($your_array);
You can replace your foreach loop's content with something like this:
foreach ($user_medicine_times as $user_medicine_key => $user_medicine_value) {
$user_medicine_times[$user_medicine_key]['time'] = json_encode($user_medicine_value['time'], true);
}
Maybe the json encode fails because your time array contains an stdClass Object. Try to convert this like that :
$result = array();
foreach ($user_medicine_value['time'] as $value) {
$result['event_type'] = $value->event_type;
$result['time'] = $value->time;
}
$user_medicine_value['time'] = $result;
Because, in every iteration, the value is not being saved anywhere,
You have two options here, either make new array having time key with json_encode() or pass the value by reference as shown below.
foreach ($user_medicine_times as $user_medicine_key => &$user_medicine_value) {
^
$user_medicine_value['time'] = json_encode($user_medicine_value['time'], true);
}

Laravel 5 pagination issue

I am working with laravel 5 pagination and getting some problems.
Here is my code DB::table('users')->paginate(2) and it returns an object of LengthAwarePaginator Like this :
Illuminate\Pagination\LengthAwarePaginator Object
(
[total:protected] => 4
[lastPage:protected] => 2
[items:protected] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 3
[Name] => shivani
[email] => shivani.ruhela#hotmail.com
[password] => $2y$10$iD1by60rSBvrMjuqwmYLseNIrd4jmvK8sXUEYiXfDjJVcBD02jEbK
[updated_at] => 2015-04-27 05:17:34
[created_at] => 2015-04-27 05:17:30
[remember_token] => guVL5RdcmRXedSBMsfSZSlCeFPfRjEq8vSNQNjtED2ytBHaPCZ3N8G3dmj6C
)
[1] => stdClass Object
(
[id] => 4
[Name] => shivani
[email] => shivani2.ruhela#hotmail.com
[password] => $2y$10$iD1by60rSBvrMjuqwmYLseNIrd4jmvK8sXUEYiXfDjJVcBD02jEbK
[updated_at] => 2015-04-27 05:17:34
[created_at] => 2015-04-27 05:17:30
[remember_token] => guVL5RdcmRXedSBMsfSZSlCeFPfRjEq8vSNQNjtED2ytBHaPCZ3N8G3dmj6C
)
)
)
[perPage:protected] => 2
[currentPage:protected] => 1
[path:protected] => http://localhost/shivani/public/check/user-list
[query:protected] => Array
(
)
[fragment:protected] =>
[pageName:protected] => page
)
My problem is that I want to iterate through this object and retrieve items object that holds database data i.e. name , email etc.
But I don't know how to access protected members from the returned object.
$data = DB::table('users')->paginate(2)->toArray()
Try this.. it might work for you
You can just use the returned object like it where an array:
$users = DB::table('users')->paginate(2);
foreach($users as $user){
echo $user->email;
}
You can access paginated objects same way you can access any other collection. For example
foreach(DB::table('users')->paginate(2) as $object) {
echo $object->name;
}

PhP - Search massive array for value, return parent key.

Q: How to Search Massive Multi-Dimensional Array for Single Value, and Return Parent Array?
I have this massive json that represents all of the achievements in WoW.
http://us.battle.net/api/wow/data/character/achievements
I converted it into an array using json_decode. This then leaves me with a very massive array that I need to search all of its levels until I find a specific value, I then need to return the parent array of that value.
ex:
This is one small part of the decoded array.
[0] => Array
(
[id] => 7385
[title] => Pub Crawl
[points] => 10
[description] => Complete the Brewmaster scenario achievements listed below.
[reward] => Reward: Honorary Brewmaster Keg
[rewardItems] => Array
(
[0] => Array
(
[id] => 87528
[name] => Honorary Brewmaster Keg
[icon] => inv_holiday_brewfestbuff_01
[quality] => 3
[itemLevel] => 90
[tooltipParams] => Array
(
)
[stats] => Array
(
)
[armor] => 0
)
)
[icon] => inv_misc_archaeology_vrykuldrinkinghorn
[criteria] => Array
(
[0] => Array
(
[id] => 20680
[description] => Spell No Evil
[orderIndex] => 0
[max] => 1
)
[1] => Array
(
[id] => 20681
[description] => Yaungolian Barbecue
[orderIndex] => 1
[max] => 1
)
[2] => Array
(
[id] => 20682
[description] => Binan Village All-Star
[orderIndex] => 2
[max] => 1
)
[3] => Array
(
[id] => 20683
[description] => The Keg Runner
[orderIndex] => 3
[max] => 1
)
[4] => Array
(
[id] => 20684
[description] => Monkey in the Middle
[orderIndex] => 4
[max] => 1
)
[5] => Array
(
[id] => 20685
[description] => Monkey See, Monkey Kill
[orderIndex] => 5
[max] => 1
)
[6] => Array
(
[id] => 20686
[description] => Don't Shake the Keg
[orderIndex] => 6
[max] => 1
)
[7] => Array
(
[id] => 20687
[description] => Party of Six
[orderIndex] => 7
[max] => 1
)
[8] => Array
(
[id] => 20688
[description] => The Perfect Pour
[orderIndex] => 8
[max] => 1
)
[9] => Array
( re
[id] => 20689
[description] => Save it for Later
[orderIndex] => 9
[max] => 1
)
[10] => Array
(
[id] => 20690
[description] => Perfect Delivery
[orderIndex] => 10
[max] => 1
)
)
[accountWide] =>
[factionId] => 2
)
I am attempting to create a function where I can just simply enter the achievement ID, which in this exmple is 7385, and have the parent array which would be [0] => Array (...); returned, so i can then grab the achievement details from that array.
I am not sure if this is really a proper question, as I am not sure as where to start.
So far I have just started breaking the original massive array down into its 10 equally as massive categories, and then searching them each individually, but I would like to just be able to search the main array once instead of searching each category array individually.
ex:
$allAchieves = file_get_contents('http://us.battle.net/api/wow/data/character/achievements');
$allAchieves = json_decode($allAchieves, true);
$generalAchieves = $allAchieves[achievements][0][achievements];
$quests = $allAchieves[achievements][1][categories];
$explorationAchieves = $allAchieves[achievements][2][categories];
$pvp = $allAchieves[achievements][3][categories];
$dungeonAndRaids = $allAchieves[achievements][4][categories];
$professions = $allAchieves[achievements][5][categories];
$reputation = $allAchieves[achievements][6][categories];
$scenarios = $allAchieves[achievements][7][categories];
$worldEvents = $allAchieves[achievements][8][categories];
$petbattle = $allAchieves[achievements][9][categories];
$featsOfStrength = $allAchieves[achievements][10][categories];
Hopefully someone can help, as the other threads I have seen sofar on array searching seem too simple to be of any help as the arrays they are dealing with are nothing to the size of the one I have here.
Thanks for the suggestions, but I solved the issue using a different approach found here:
http://us.battle.net/wow/en/forum/topic/8892160022?page=1#4

Codeigniter: Referencing a specific array index returned in model result

I'm still trying to wrap my head around passing db query results from a model back to controller and finally to a view. I seem to be getting the data to the right place, I'm just not sure how to best access the resulting array of objects in the view.
Specifically, I'm trying to query my db for the most recent 7 distinct dates that someone has submitted a link. I get back an array of dates, and then for each of those dates I do a query for all links submitted on that date and store the results in an array. Then in the view, for each of those distinct dates, I show a header (the date), immediately followed by the links associated with it.
The array that come from my distinct date query ($link_headers):
Array (
[0] => stdClass Object([added_date] => 2011-08-11)
[1] => stdClass Object([added_date] => 2011-05-03)
[2] => stdClass Object([added_date] => 2011-04-21)
[3] => stdClass Object([added_date] => 2011-04-10)
[4] => stdClass Object([added_date] => 2011-03-04)
[5] => stdClass Object([added_date] => 2011-02-28)
[6] => stdClass Object([added_date] => 2011-02-22)
)
The array that comes from my query for actual links submitted ($links_result):
Array
(
[0] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1178
[link_url] => http://www.amazon.com/Silicone-Rubber-CASSETTE-Design-IPHONE/dp/B004YDJWOY
[link_name] => Silicone Skin BLACK CASSETTE TAPE
[link_notes] => iPhone case... probably won't fit in my dock.
[added_date] => 2011-08-11
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[1] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1177
[link_url] => http://snorby.org/
[link_name] => Snorby - Snort front-end
[link_notes] =>
[added_date] => 2011-05-03
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[2] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1176
[link_url] => http://www.nytimes.com/2011/04/17/business/17excerpt.html?_r=4&pagewanted=1&ref=business
[link_name] => Corner Office - The 5 Habits of Highly Effective C.E.O.s
[link_notes] => Sounds a lot like what Nathanial said...
[added_date] => 2011-04-21
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[3] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1175
[link_url] => http://chezlarsson.com/myblog/2010/06/panduro-concrete-challenge-3.html
[link_name] => Concrete book-ends
[link_notes] => Cool look...
[added_date] => 2011-04-10
[flag_new] => 1
[rating] => 4
[public] => 1
)
[1] => stdClass Object
(
[user_id] => 2
[link_id] => 1174
[link_url] => http://themeforest.net/item/reciprocity-photo-blog-gallery/154590
[link_name] => Site Templates - Reciprocity - Photo Blog
[link_notes] =>
[added_date] => 2011-04-10
[flag_new] => 1
[rating] => 5
[public] => 1
)
)
[4] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1173
[link_url] => http://lifehacker.com/#!5771943/the-always-up+to+date-guide-to-jailbreaking-your-ios-device
[link_name] => The Always Up-to-Date Guide to Jailbreaking Your iOS Device
[link_notes] =>
[added_date] => 2011-03-04
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[5] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1172
[link_url] => http://lifehacker.com/#!5754463/how-to-jailbreak-your-ios-421-device
[link_name] => How to Jailbreak Your iOS 4.2.1 Device
[link_notes] =>
[added_date] => 2011-02-28
[flag_new] => 1
[rating] => 4
[public] => 1
)
)
[6] => Array
(
[0] => stdClass Object
(
[user_id] => 2
[link_id] => 1171
[link_url] => http://www.bitplumber.net/2010/10/a-cassandra-hardware-stack-dell-c1100s-ocz-vertex-2-ssds-with-sandforce-arista-7048s/
[link_name] => A Cassandra Hardware Stack
[link_notes] =>
[added_date] => 2011-02-22
[flag_new] => 1
[rating] => 3
[public] => 1
)
)
)
... all seems fine enough. But my problem comes from my view, where I'm trying to build the HTML as described above. A simplified view of the code I'm trying to get to work is as follows:
foreach ($link_headers as $header) {
echo "INDEX: ". $links_headers .", ADDED DATE: ". $header->added_date ."<BR>";
foreach ($links_result[$link_headers] as $result){
echo $result->added_date ."<BR>";
echo $result->link_name ."<BR><BR>";
}
}
So, I'm trying to use the index of the first one to tell my foreach loop which index of the second array to loop through and get the content. Clearly I'm misusing the $links_result[$link_headers] variable(s) but I left it in to show what I was trying to do.
Any help is very much appreciated!
Michael
I dont use CodeIgniter but whether within th framework or from PHP i would just grab it all in one go and then the issue with the indexes becomes moot:
SELECT * FROM model_table mt WHERE mt.added_date IN (
SELECT DISTINCT md.added_date from model_table md
ORDER BY md.added_date DESC
LIMIT 7
) ORDER BY mt.added_date DESC
That should get you an array of models ordered by date limted to the 7 most recent dates. So then its just a matter of choosing when to display a header:
$current = null;
foreach($links as $link) {
if($link->added_date !== $current) {
// show the header and set current to the current date
$current = $link->added_date;
echo 'HEADER: Added on ' . $current . '<br />';
}
echo $row->added_date ."<BR>";
echo $row->link_name ."<BR><BR>";
}

Categories