php basic array question - php

I'm having trouble getting the value of the last insert Id from doctrine. I did a bit of research and I found that the following should work:
//save the store now
$store = new Store();
$store->url = $this->form_validation->set_value('website');
$store->save();
$store_id = $store->identifier();
echo print_r($store->identifier());
Returns
Array ( [id] => 1000034 )
How do I pull just the value (1000034) out of the array and set it in a variable that I can use elsewhere?

Your $store_id is an associative array with a single value keyed by "id" so:
$id = $store_id['id'];

Related

Php access variable

In PHP i have array variable from another function like this
$v->params:
(
[{"username":"myusername","email":"myemail#gmail_com","phone":"0123456789","password":"abc123","fullname":"myfullname","register_ip":"127_0_0_1","country":"Qu\u1ed1c_Gia","birthday":"N\u0103m_sinh","gender":"male","bank_code":"Ng\u00e2n_h\u00e0ng","ip":"127_0_0_1","os":"Windows_10","device":"Computer","browser":"Mozilla_Firefox_77_0"}] =>
)
Now i want to access to it item, how can i code to access item value like this:
$password = $v->params->password; //myemail#gmail_com
I new with PHP thank you all
The data seems the wrong way round as it's the key of the array rather than a value.
So using array_keys()[0] to get the first key and then json_decode this...
$data = json_decode(array_keys($v->params)[0]);
you can then use the $data object to get at the values...
echo $data->username;

how to do replace and explode for array input in laravel

I am beginner to laravel. I am trying to access array input from my API request.
For postman, I have array as key called file_name_list and its value like ["m_profile.png","aa_image.jpg","new_pan.jpg"]. I want to access that array in my controller. That values should go into 3 seperate variables like
$profile = m_profile.png
$aadhar = aa_image.jpg
$pan = new_pan.jpg
For that I am trying to use replace and explode functions in controller.
$filenamelist1 = Str::replaceArray(array(' ','"', '[',']'),[""], $request->file_name_list);
$filename_str = explode(",",$filenamelist1);
After this I want to store values from explode array to 3 variables as mentioned above using for loop
But I am facing problems like in Str::replaceArray 2 parameter should be array and for explode 2 parameter should be string.
How should I use replace and explode to get required result? please guide. Thanks in advance.
You can use list method like below:
list( $profile , $adhar, $pan) = $request->file_name_list;
Check the Docs
If Your array size fixed then you assign that by using list
list( $profile,$adhar,$pan) = $request->file_name_list;
<?php
$value = '["m_profile.png","aa_image.jpg","new_pan.jpg"]';
$value = str_replace("[","",$value); // clean [
$value = str_replace("]","",$value); // clean ]
$arrayValues = explode('","',$value); // explode with ","
print_r($arrayValues);
output
Array (
[0] => "m_profile.png
[1] => aa_image.jpg
[2] => new_pan.jpg"
)
Replace " when you access the values

How to get PHP session variable deeply nested inside array?

Have tried a number of permutations to get a value from a PHP session. The session is an array of objects I think with key value pairs. This is the structure as outputted by a key/value foreach loop
Array
(
[laboratory_roster] => Array
(
[employee_entrance] => stdClass Object
(
[step] => employee_entrance
[employee_first_name] => asdfasd
[employee_last_name] => fasdfasdfv
[employee_access_code] => valid
[employee_email] => blah#blah.com
[employee_state_origin] => NY
[employee_kit_for_whom] => employee_kit_for_employee
)
)
)
This is the foreach loop that I wrote to display the output above:
foreach($_SESSION['laboratory_roster']['employee_entrance'] as $key=>$value)
{
// and print out the values
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
What I wish to do is simply assign the value of the innermost value to a variable. Nothing works. Have tried:
$first_name = $_SESSION['employee_entrance']['employee_first_name'];
and this...
$first_name = $_SESSION['employee_entrance'][1];
and this...
$first_name = $_SESSION['laboratory_roster']['employee_entrance']['employee_first_name'];
and this...
$first_name = $_SESSION['laboratory_roster']['employee_entrance']['employee_first_name'][0];
Nothing works! It's probably so simple how to get the innermost value into a PHP variable, but I am not getting it. Thanks for help!
Two ways:
1 Convert to stdClass: $stdClass = json_decode(json_encode($_SESSION)); Then access with [].
2 Convert to array: $array = json_decode(json_encode($_SESSION), true); Then access with ->.
Try:
$first_name = $_SESSION['laboratory_roster']['employee_entrance']->employee_first_name;
As employee_entrance is an object (instance of stdClass) and not an array.

php how get a value of json?

I have this data
$result = json_decode(curl_exec($ch));
print_r($result);
// stdClass Object ( [d] => {"id":10} )
$id = ?;
How can I get a value of id?
Since $result is an object, you have to use property notation to access its components.
$id = json_decode($result->d)->id;
You need the extra json_decode because the value of $result->d is another JSON string, not an object or array.
You would get value with following
$obj_result = json_decode($result->d);
echo $obj_result->id;
By this way you will get id value 10

How can I get an indexed array from query result in Kohana 3.2?

How can I get the INDEXED array result?
$qry1 = DB::select('name')->from('people')->execute();
$assoc_array = $qry1->as_array();
$object = $qry1->as_object();
// $indexed_array = [...]
Only for learning purposes, thanks.
It's like:
$indexed_result[0]; // Name
// $indexed_result[1];
// $indexed_result[2];
Do you want to get an array of names like array(0 => 'John', 1 => 'Sam')?
You should call $names = $gry1->as_array(NULL, 'name');
http://kohanaframework.org/3.3/guide/database/results#select-asobject-and-asassoc
The method as_assoc() will remove the object name and return the
results set back to an associative array. Since this is the default,
this method is seldom required.
So just do your execution.
But if you want only one row take a look at the current() method.

Categories