How to get the required value displayed from the given array? - php

From below array i want the value of "_sql" to be displayed or we can I want to echo the "_sql" value, so what should be the syntax to display it in PHP code?
JTableMenu Object
(
[lft] =>
[rgt] =>
[home] =>
[_tbl] => #__menu
[_tbl_key] => id
[_db] => JDatabaseMySQL Object
(
[name] => mysql
[_nullDate] => 0000-00-00 00:00:00
[_nameQuote] => `
[_sql] => INSERT INTO `jos_menu` ( `id`,`menutype`,`name`,`alias`,`link`,`type`,`published`,`componentid`,`parent`,`ordering`,`checked_out`,`checked_out_time`,`brow............
[_errorNum] => 0
)
)
In the code the above array is stored in $row, so i have written an echo statement to display the required value like this: echo $row['_db]['_sql']; please suggest where and why I am getting wrong Why I cannot get the value in "_sql" index?

I got the Solution I did this
echo $row->_db->_sql;
And I got the output...
Thanks Pranav

Related

PHP: Removing string count of entire array from serialized array

I have a JSON array that I'm saving in a PHP variable. I'm then serializing that array using serialize($variable) and saving it to the dB with the built in Wordpress function update_post_meta().
The problem I'm having is that the entire serialized array is wrapped with a string count. Like so, currently being saved as:
s:332:"a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}";
I need to save it without the string count for the entire array. Desired output:
a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}};
Any help with this is, as always, greatly appreciated.
It seems that your array is serialized twice and that's what gives you the addendum ... I have taken your serialize data and un serialized it twice and it came back as you wanted :
<?php
$ser = 's:332:"a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}";';
$arr = unserialize($ser);
echo '<pre>';
print_r($arr); /* Print after one unserialize */
echo '<pre>';
print_r(unserialize($arr)); /* Print with unserialize to the once unserialized*/
Will return:
a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}
Array
(
[0] => Array
(
[type] => weeks
[cost] => 3
[modifier] =>
[base_cost] => 2
[base_modifier] =>
[from] => 1
[to] => 4
)
[1] => Array
(
[type] => persons
[cost] => 6
[modifier] =>
[base_cost] => 5
[base_modifier] =>
[from] => 1
[to] => 2
)
)
As you can see only after two unserialize it returns back to an array... so just do one and you have what you need.

Extract data from php array

I have the following array in my Moodle plugin:
Array (
[0] => stdClass Object (
[id] => 32
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1464071400
[timefinish] => 1464102000 )
[1] => stdClass Object (
[id] => 33
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1465281000
[timefinish] => 1465311600 )
)
How to get the data. Right now, when I make:
$pluginsessiondates = $DB->get_record('plugin_sessions', array('id'=>$sessionid));
I get only data from the frist array [0]
How to get the data from every array key and then the single values? Thanks in advance.
The Moodle DB functions are for getting data out of the database, rather than from an array somewhere inside your plugin.
If you have an array somewhere, then you can get fields from it by writing:
echo $myarray[0]->id;
echo $myarray[1]->id;
etc.
If you are not trying to get data out of an existing array and want, instead, to get it out of the database, then $DB->get_record() will, as its name implies, get you only a single record, whereas $DB->get_records() will get you all the matching records:
$sessions = $DB->get_records('plugin_sessions', array('sessionid' => $sessionid));
foreach ($sessions as $session) {
echo $session->id;
}

how to get session variable value in php?

Inside my code i can see one session and once i printed that session i got following things in view source
Array
(
[Config] => Array
(
[time] => 1406983421
[timeout] => 10
)
[loggedIn] => 1
[user] => Array
(
[User] => Array
(
[id] => 424
[correspondence_email] =>
[terms] => Y
[old_facebook] =>
[[old_twitter] =>
[gmail_email] =>
[login_count] => 9
[last_secdeg_updated] => 2014-08-01 08:47:35
)
)
)
then i tried this line to get the value of "login_count"
echo "value".$this->Session->read("login_count");
but am not able to see this the "login_count" in echo.
am getting only this result in viewsource
value
How do i get "login_count" vale to a variable.?
echo $this->Session->read('user.User.login_count');
I hope this helps you.
Without seeing the code that writes the session, I am suspecting that this is what you're looking for:
echo $this->Session->read('user.User.login_count');
You dont have to create any codes to perfom session operations.They are already built in the cake php api.You just need to read and write the sessions from the link i given below.
http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html Used in Controllers
http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html Used in Views

How to get value of array inside another array in php?

I made a php page in which I got the array within array value.
Here is my array:
Array (
[events] => Array ( [0] => 5 [1] => 7 [2] => 8 )
[fromdate] =>
[todate] =>
[description] =>
[subject] =>
[fromname] =>
[replyto] =>
[senddatetime] =>
[timezone] => America/Los_Angeles
[message] =>
[submit_skip] => Continue
)
This array is the POST data from previous page. I want to get events value in a variable. How can I do this?
I use this for getting events value but it gives me the array but I want to get first event value
echo $eventid=$_POST['events'];
To echo:
echo $_POST['events'][0];
To assign it to a variable:
$eventid = $_POST['events'][0];
To assign the array to a variable and get the value from the new array:
$events = $_POST['events'];
$eventid = $events[0];
[0] represents the key of the first value since it is set to be 0.
Output:
echo $_POST['events'][0];
Assign to variable:
$eventid = $_POST['events'][0];

Rewrite array keys by matching it to first child key, possible in php?

I'm wondering if there is an easy way to match array key to logo_id?
If I cannot find a way to do this, I will need to use array search which can become quite slow with an array of 200 items. Right?
p.s. this is result returned by mysqli fetch result call. Maybe this can be modified to provide array which I need?
Array
(
[0] => Array
(
[logo_id] => 1
[logo_name] => beeline
[logo_level] => 1
[logo_image_path] => logos/1.png
[logo_value] => 2
[logo_hints] =>
)
[1] => Array
(
[logo_id] => 2
[logo_name] => geocell
[logo_level] => 1
[logo_image_path] => logos/2.png
[logo_value] => 4
[logo_hints] =>
)
[2] => Array
(
[logo_id] => 3
[logo_name] => google
[logo_level] => 1
[logo_image_path] => logos/3.png
[logo_value] => 5
[logo_hints] =>
)
[3] => Array
(
[logo_id] => 5
[logo_name] => coca cola
[logo_level] => 1
[logo_image_path] => logos/5.png
[logo_value] => 2
[logo_hints] =>
)
)
Did I explain it good? phh, sorry for bad wording.
this is result returned by mysqli fetch result call. Maybe this can be modified to provide array which I need?
Yes. I assume you mean mysqli_result::fetch_all. Use mysqli_result::fetch_row in a loop instead and construct your array manually with whatever keys you like to.

Categories