curl php i need to read [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
Array
(
[result] => 1
[data] => Array
(
[0] => Array
(
[uniq] => 123456789
[name] => rig2
[description] => rig2
)
[1] => Array
(
[uniq] => 987654321
[name] => rig1
[description] => rig1
)
)
)
Plese give me exsample how to echo(print) only [uniq] in php ?

You can use array_column function to take out all the values corresponding to [uniq] key.
$uniq_values = array_column($input_array['data'], 'uniq');
print_r($uniq_values); // print them out
From documentation:
array_column() returns the values from a single column of the input,
identified by the column_key.
input is A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is
provided, then public properties can be directly pulled. In order for
protected or private properties to be pulled, the class must implement
both the __get() and __isset() magic methods.
column_key is The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects.

Related

Collect all values from nested array row of specific key [duplicate]

This question already has answers here:
How to get an array of specific "key" in multidimensional array without looping [duplicate]
(4 answers)
Closed 1 year ago.
Need to create a list that consists of all values stored in the array row of a specific key (product_id). Currently, doing a print_r of my $bestsellers variable produces the following array:
Array
(
[0] => stdClass Object
(
[product_id] => 178
[order_item_qty] => 9
)
[1] => stdClass Object
(
[product_id] => 233
[order_item_qty] => 4
)
[2] => stdClass Object
(
[product_id] => 179
[order_item_qty] => 1
)
)
Other SO answers led me to try:
$ids = array_column($bestsellers, 'product_id');
...but that produced an empty array, I guess because the row I’m trying to grab is nested in there? With that in mind I tried
foreach($bestsellers as $bestseller) {
$ids = array_column($bestsellers, 'product_id');
}
...which produced no result at all.
Hopefully someone can help clue me in as to where I’m going wrong. Thanks!
The nested values are objects, not arrays (can't you see stdClass Object in the output?). array_column is for 2-dimensional arrays. You need to access the properties using object syntax.
$ids = array_map(function($x) { return $x->product_id; }, $bestsellers);
For future reference, array_column will work for this in PHP 7, so you must be using PHP 5.
For PHP 7, your code
$ids = array_column($bestsellers, 'product_id');
will do what you want it to.
See the difference here on 3v4l.org.

get value from array with std object (php) [duplicate]

This question already has answers here:
stdClass object array inside of another array
(3 answers)
Closed 5 years ago.
my code in PHP :
$stuff = array($this->complaint_model->get_reply($id)->result());
print_r($stuff);
result :
Array (
[0] => Array (
[0] => stdClass Object (
[id_customer] => 21
[id_complaint] => 2
[id] => 4
[nama_customer] => Muhammad Bima Zehansyah
[from] => Admin
[balasan] => coba update
)
)
)
my question is , how to get value [nama_customer] ? thx before guys
Try this
$stuff = array($this->complaint_model->get_reply($id)->result());
echo $stuffVal = $stuff[0][0]->nama_customer;
Get the value like this
$stuff[0][0]->nama_customer
Here you have multidimensional array object that's why you need to first Travers two array like $stuff[0][0] and then the object like $stuff[0][0]->nama_customer
Actually you do not need to put the result into additional array and it would be wise to check if the result is not empty.
So you just take a first item from your result (which is an object) and call the parameter nama_customer
$stuff = $this->complaint_model->get_reply($id)->result();
if (!empty($stuff[0]))
echo $stuff[0]->nama_customer;

Exclude items from JSON string that is generated from PHP Array

I have a PHP function $users->getFullUserList('json') that returns a JSON string holding a Userlist of user data which is built from a PHP Array using PHP's json_encode($this->userListArray)
The PHP Array $this->userListArray looks something like this below before being converted into a JSON string...
Array
(
[user1] => Array
(
[id] => 1
[user_name] => jasondavis
[first_name] => Jason
[last_name] => Davis
[is_admin] => 1
[email_address] => jasonyrty#tyrtl.com
[gravatar_id] => 31b64e4876d603ce78e04102c67d6144
)
[1702c3d0-df12-2d1b-d964-521becb5e3ad] => Array
(
[id] => 1702c3d0-df12-2d1b-d964-521becb5e3ad
[user_name] => Jeff
[first_name] => Jeff
[last_name] => Mosley
[is_admin] => 1
[email_address] => fgfgh#rtyrtyre.com
[gravatar_id] => 5359bf585d11c5c35602f9bf5e66fa5e
)
)
What I need help with is making my function $users->getFullUserList('json') to allow another parameter to be injected which will be an array of keys which should NOT be part of the final JSON String
For example if I want to generate my JSON string from the PHP Array and have it exclude user email_address and first_name keys from the JSON string.
I do not want to simply remove them from the PHP Array as I will need access to them in PHP later on. I just want the keys in my new passed in array to be excluded from the generated JSON and still remain in the PHP Array.
$excludeKeysFromJson = array(
'first_name',
'email_address'
);
$users->getFullUserList('json', $excludeKeysFromJson)
I realize the simple solution would be to clone my $this->userListArray Array and then remove the matching keys from the $excludeKeysFromJson Array and generate the JSON string from the new cloned Array.
I am hoping to find a better performance method that doesn't require cloning my huge userlist array and keeping 2 copies of the userlist array with 1 missing a couple keys.
Is there a better way?
You can create an array with keys filtered out using array_diff_key and array_flip (to turn the exclusion array values into keys)
Something like this...
public function getFullUserList($format, array $exclude = []) {
$filtered = array_map(function($user) use ($exclude) {
return array_diff_key($user, array_flip($exclude));
}, $this->userListArray);
switch($format) {
case 'json' :
return json_encode($filtered);
default :
return $filtered;
}
}
Demo

Take value from array which is in array [duplicate]

This question already has answers here:
PHP - Accessing Multidimensional Array Values
(4 answers)
Closed 8 years ago.
I've got this url:
http://web.com/script.php?identifiers%5Bmc%5D%5Bnick%5D=name1&identifiers%5Bcs%5D%5Bnick%5D=name2&identifiers%5Bcs%5D%5Bpassword%5D=mypass
so i will get array like this:
[identifiers] => Array
(
[mc] => Array
(
[nick] => name1
)
[cs] => Array
(
[nick] => name2
[password] => mypass
)
)
How do I take value name1 and put into variable $mc_name?
That's a simple array containing another array so you can simply specify multiple indexes for included array:
$mc_name = $_GET['identifiers']['mc']['nick'];
To better understand how it works think of it like assigning each array first to a variable like:
$identifiers = $_GET['identifiers'];
$mc_array = $identifiers['mc'];
$mc_name = $mc_array['nick'];
which will essentially do the same thing at once, without the need to specify multiple variables and arrays.
Start with:
identifiers = $_GET['identifiers']
If you know the key names, then simply:
$mc_name = $identifiers['mc']['nick']
If you know it's the first value or the first value, then you can:
$mc_name = array_shift($identifiers); // get the 'mc' array
$mc_name = array_shift($identifiers); // get the 'nick' value
Not that array_shift will actually remove the elements from the original array.

Extract values from php array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I am using SOAP to get data from the server and in response i am getting a php array like this
Array
(
[BookResult] => stdClass Object
(
[PNR] => 5WPODU
[BookingId] => 31149
[Status] => stdClass Object
(
[StatusCode] => 03
[Description] => Fare is not available at the time of booking
[Category] => BK
)
[SSRDenied] => N
[ProdType] => Flight
)
)
All i want to know is how can i extract "PNR" and "StatusCode" value in separate variables so that i can store them in database.
Tried this not working
$p = (object) $array;
echo $p->StatusCode;
Try this:
$PNR = $array["BookResult"]->PNR;
$StatusCode= $array["BookResult"]->Status->StatusCode;
$array is an array. So first dive is $array['BookResult'].
BookResult is stdClass instance so next goes $array['BookResult']->Status (get object's property).
Status is also stdClass instance so get it's property: $array['BookResult']->Status->StatusCode
var_dump($array['BookResult']->PNR);
var_dump($array['BookResult']->Status->StatusCode);
Assuming results are being stored in $array
echo $array['BookResult']->Status->StatusCode;
echo $array['BookResult']->PNR;

Categories