How to get the single value from the array? [duplicate] - php

I stored some data to an array using this code
$this->data['result'] = $this->mymodel->search($keyword);
when I print my result by using print_r($this->data);
it shows like this
Array ( [base_url] => http://localhost/pelikanartline.com/ [result] => Array ( [0] => stdClass Object ( [product_id] => 2 [product_code] => 167007 [product_name] => ARTLINE VIVIX HIGHLIGHTERS 10PK YELLOW [product_discription] => some description [xylene_free] => y [rohs_compliant] => y [product_features] => some text ) ) )
now I want to store product_id value to a variable like pro_id. please help me to do this.

Try this:
$pro_id = [];
foreach($this->data['result'] as $key => $val){
$pro_id[] = $val->product_id;
}
print_r($pro_id);

If your model returns only one record, you can use this shortcut:
$pro_id = $this->data['result'][0]->product_id;

Related

php get values from array json output

When I use the code below:
print_r($jsoni);
$badge_url = "http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=841370%3Fkey&steamids=76561198108211948&fbclid=IwAR0B4wUlosbqFElHBJw-AkLwb3mGsv42xKdtrEAarDmD97Ur3AprrkW4tCk";
$jsoni = json_decode(file_get_contents($badge_url), true);
I get the following as a result:
Array
(
[achievementpercentages] => Array
(
[achievements] => Array
(
[0] => Array
(
[name] => GAME_GREEN_LIGHT
[percent] => 70.9000015259
)
[1] => Array
(
[name] => CAREER_EARN_BADGE
[percent] => 62.2999992371
)
How can I get it so that it only shows the name and the percentage?
print_r($jsoni['achievementpercentages']['achievements'])
You could loop through the decoded data to create an associative array with the achievement names as keys and the percentages as values:
$achievements = [];
foreach($jsoni['achievementpercentages']['achievements'] as $achievement)
$achievements[$achievement['name']] = $achievement['percent'];
Which outputs:
Array
(
[GAME_GREEN_LIGHT] => 70.9000015259,
[CAREER_EARN_BADGE] => 62.2999992371
)
So im now using the following code.
<?php
$achievements = [];
foreach($jsoni['achievementpercentages']['achievements'] as $achievement)
$achievements[$achievement['name']] = $achievement['percent'];
print_r($achievements);
?>
wich results in
Array ( [GAME_GREEN_LIGHT] => 70.9000015259 [CAREER_EARN_BADGE] => 62.2999992371
how do i get it to show a list from top to bottom instead of in a cluster of text?
Thanks for the help!

Print a specific value from array in ci

I stored some data to an array using this code ($rval['arr']):
$id = $this->input->get("id");
$rval['arr'] = $this->General_model->details($id);
When I print my result by using print_r($rval['arr']); it shows like this:
Array ( [0] => Array ( [id] => 54 [name] => Rajib [address] => DumDum [mobile] => 9865321245 [doj] => 21-2-2010 [fare] => 1245 [img_name] => Penguins.jpg ) )
Now I want to store the address value to a variable like add. How can I do this?
Try this....
$address=$rval['arr'][0]['address'];
demo
You can also do it in a foreach() loop:
foreach($rval['arr'] as $array){
echo $array['name'];
echo $array['address'];
echo $array['mobile'];
}

Retrieving correct record from multidimentional array

I'm having a mental freeze moment. If I have an array in the following format:
$myData = Array
(
[0] => stdClass Object
(
[id] => 1
[busID] => 5
[type] => SMS
[number] => 5128888888
)
[1] => stdClass Object
(
[id] => 2
[busID] => 5
[type] => APP
[number] => 5125555555
)
[2] => stdClass Object
(
[id] => 4
[busID] => 5
[type] => APP
[number] => 5129999988
[verified] => 1
[default] => 0
)
)
And I only have an var for ID of the record, how do I retrieve the rest of the detail for that set.
$myID = 2;
// get number 5125555555 and it's type
echo $myData[][$myID]['number']; // ???
The way you have your data arranged your going to have to loop through your array to identify the object corresponding to $myID.
foreach($myData as $object) if($object->id == $myID) echo $object->number;
The alternative is to arrange your $myData as an associative array with the id field as the key. Then you could access it simply with $myData[$myID]->number.
Actually it's an array that contains StdClass objects , try looping over $myData and access each attribute :
foreach ( $myData as $data )
{
print_r($data->id);
// ...
}
You can avoid loop by using following logic:
<?php
$myID = 2;
$myData = json_decode(json_encode($myData),1); // Convert Object to Array
$id_arr = array_column($myData, 'id'); // Create an array with All Ids
$idx = array_search($myID, $id_arr);
if($idx !== false)
{
echo $myData[$idx]['type'] . ' -- ' . $myData[$idx]['number'];
}
?>
Working Demo
Note: array_column is supported from PHP 5.5.
For lower versions you can use this beautiful library https://github.com/ramsey/array_column/blob/master/src/array_column.php
You can create a custom function to achieve this, you need to pass the array and id whose details you want and the function will return the array with matching id, like below
function detailsById($myData,$id){
foreach($myData as $data){
if($data->id == $id){
return $data;
}
}
}
Just call this function with your array and id..
$data=detailsById($myData,2);
echo "<pre>";print_r($data);
This will give you :
stdClass Object
(
[id] => 2
[busID] => 5
[type] => APP
[number] => 5125555555
)
And further to print 'number' and 'type' use $data array
$data['type'];
$data['number'];

Retrieve name-value pair into PHP array

I need some help. I have a variable containing this string;
[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]
I need to retrieve the id and value for each pair and make it any array in PHP.
You can use json_decode and pass the second param as true so it returns an array like this
$json = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"},{"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$decoded = json_decode($json,true);
print_r($decoded);
Working Example
Output would be
Array
(
[0] => Array
(
[id] => 17
[value] => 123456789
)
[1] => Array
(
[id] => 18
[value] => 2012-06-13
)
[2] => Array
(
[id] => 19
[value] => Kampala
)
[3] => Array
(
[id] => 20
[value] => 1
)
.......
)
Which you can loop through using foreach like.
foreach($decoded as $de){
// access id with $de['id']
// access value with $de['value']
}
You have got an json string. You can convert it to an array by using function json_decode
Check this code .
$str = '[{"id":"17","value":"123456789"},{"id":"18","value":"2012-06-13"}, {"id":"19","value":"Kampala"},{"id":"20","value":"1"},{"id":"21","value":"500g"},{"id":"22","value":"Emirrets"},{"id":"23","value":"q"},{"id":"24","value":"q"},{"id":"25","value":"q"},{"id":"26","value":"q"},{"id":"27","value":"q"},{"id":"28","value":"q"},{"id":"29","value":"2"},{"id":"30","value":"987654321"},{"id":"45","value":"1"},{"id":"46","value":"1"}]';
$array = json_decode($str);
foreach($array as $temp){
echo "ID : ".$temp->id."\t Value: ".$temp->value;
echo "<br />";
}

insert item into array

I have a result array that comes back from CodeIgniter looking like this:
Array
(
[ILS] => ils
[0] => Array
(
[laser_id] => 1
[sort_order] => 1
[laser_name] => ILS12.75
[laser] => ils1275
[image] => ils1275
[link] => ils1275
)
[1] => Array
( .. )
etc.
}
I used array_merge to get the ILS item into the array, but I need it to get it into the array[0]. I also need to get a similar item into array[1] and so on in the result array. How do I do this? Here is how I got the ILS item in:
function get_laser_configs()
{
$this->db->order_by('sort_order');
$query = $this->db->get('all_lasers')->result_array();
$ils = array('ILS' => 'ils');
return array_merge($ils, $query);
}
No need for array_merge, simply do:
$query[0]['ILS'] = 'ils';
If you want to do it for each element in the array:
foreach ($query as $key => $q) {
$query[$key]['ILS'] = 'ils';
}

Categories