I have this multidimensional array and I want to access a specific value without doing loop.. is it possible?
here's the array:
Array
(
[0] => stdClass Object
(
[akeebasubs_user_id] => 205
[user_id] => 268
[isbusiness] => 0
[businessname] => sci555
[occupation] =>
[vatnumber] =>
[viesregistered] => 0
[taxauthority] =>
[address1] => Ma. Cristina St.
[address2] => Negros Oriental
[city] => Dumaguete
[state] => IA
[zip] => 6200
[country] => BS
[params] => {"work_telephone":"232424","hospital_company":"sci5","company_introductory":"test","organization_type":"","applicant_url":"www","user_title":"","year_established":"","parent_company":"","r_01":"","r_02":"","r_03":"","r_04":""}
[notes] => <p>test</p>
)
)
what I want is to access the user_id which is 268 directly.
You will need to do the following:
var_dump($array[0]->user_id);
$arrayis a one-entry array that contains an stdClass object (you access an object property by using ->).
Related
I have been trying to extract the following data for each student from a large multidimensional array (Shown further down)
Data -> gender,
Data -> grade,
Data -> name -> first,
Data -> name -> last,
Data -> name -> middle,
Data -> student_number,
Data -> ID
I have tried various options through searching including Slice, Splice, and a for loop.
Every attempt has been met with failure on all or some of the data. I have never been able to get to the 3rd nested data of First, Middle, and Last name.
How can I take a large multidem array like this, and extract the data listed above in a foreach loop so I can import it into a database? I feel like it is alot simpler than I am making it. I have not included any code as I have yet to have anything that seems remotely useful.
Here is a sample array below. Thanks!
Array
(
[data] => Array
(
[0] => Array
(
[data] => Array
(
[gender] => M
[dob] => 7/17/2008
[email] =>
[grade] => 2
[schools] => Array
(
[0] =>12345
)
[school] => 12345
[created] => 2018-04-16T14:01:00.437Z
[name] => Array
(
[first] => Jacob
[last] => Smith
[middle] => Rabbitboom
)
[location] => Array
(
[zip] =>
[address] =>
[city] =>
[lat] =>
[lon] =>
[state] =>
)
[district] => 123456
[last_modified] => 2018-04-16T14:01:00.437Z
[race] =>
[hispanic_ethnicity] =>
[graduation_year] =>
[student_number] => 1234567
[credentials] => Array
(
[district_username] =>
)
[id] => 123456
)
[uri] =>
)
[1] => Array
(
[data] => Array
(
[gender] => F
[dob] => 7/17/2008
[email] =>
[grade] => 2
[schools] => Array
(
[0] =>12346
)
[school] => 12345
[created] => 2018-04-16T14:01:00.437Z
[name] => Array
(
[first] => Jason
[last] => Smith
[middle] => RobesPerrie
)
[location] => Array
(
[zip] =>
[address] =>
[city] =>
[lat] =>
[lon] =>
[state] =>
)
[district] => 123456
[last_modified] => 2018-04-16T14:01:00.437Z
[race] =>
[hispanic_ethnicity] =>
[graduation_year] =>
[student_number] => 1234568
[credentials] => Array
(
[district_username] =>
)
[id] => 123459
)
[uri] =>
)
The easiest approach might be to extract the nested data arrays and loop that:
foreach(array_column($array['data'], 'data') as $data) {
echo $data['gender'];
echo $data['name']['first'];
}
If schools is variable length then you'll need to loop that or implode(', ', $data['schools']).
I have a variable $array which includes an array of data zip_code, distance, city, and state. I want an array of just the zip_code's. If I do print_r ($array); I get the following.
Array ( [zip_codes] => Array ( [0] => Array ( [zip_code] => 02822 [distance] => 19.544 [city] => Exeter [state] => RI ) [1] => Array ( [zip_code] => 02871 [distance] => 18.965 [city] => Portsmouth [state] => RI ) [2] => Array ( [zip_code] => 02852 [distance] => 16.092 [city] => North Kingstown [state] => RI )
How can I print it, or get it into a format, that it looks like the following:
Array ( [0] => 02822 [1] => 02871 [2] => 02852 )
Please help. Thanks!
See the php array_column() function.
http://php.net/manual/en/function.array-column.php
I have a large array of hostnames and their corresponding location.
Array ( [ABC01] => Array ( [hostid] => 12345
[lat] => 123
[lon] => 123
[adr] => 126 Foo Street
[city] => Rocky Hill
[state] => Connecticut
[country] => USA )
[ABC02] => Array ( [hostid] => 12346
[lat] => 345
[lon] => 345
[adr] => 123 Foo Street
[city] => Boston
[state] => Massachusetts
[country] => USA )
[ABC03] => Array ( [hostid] => 12346
[lat] => 345
[lon] => 345
[adr] => 123 Foo Street
[city] => New York City
[state] => New York
[country] => USA )
.....)
I am comparing it to a much smaller array - same host name but it links to the IP address:
Array ( [ABC01] => Array ( [ip] => 192.168.2.1 )
[ABC02] => Array ( [ip] => 192.168.2.2 )
[ABC03] => Array ( [ip] => 192.168.2.3 )
)
I want to create the following array:
[ABC02] => Array ( [hostid] => 12346
[lat] => 345
[lon] => 345
[adr] => 123 Foo Street
[city] => Boston
[state] => Massachusetts
[country] => USA
[ip] => 192.168.2.1)
I'm trying to find the intersection of the arrays and then merge the two (find the same key and then merge). I've tried various functions but the end result is never what I want.
I managed to find a function here to merge the common keys, but it returned the IP in an array in the array containing the location fields in another array, as seen below:
Array ( [ABC02] => Array ( [0] => Array ( [hostid] => 12346
[lat] => 345
[lon] => 345
[adr] => 123 Foo Street
[city] => Boston
[state] => Massachusetts
[country] => USA
[1] => Array ( [ip] => 192.168.2.1) ) )
Is there an easier way to do this?
Pretty easy with a built-in PHP function:
$result = array_merge_recursive($large, $small);
Based on your comment to get only values that are common and then merge. This assumes all keys in $small are in $large:
$result = array_merge_recursive(array_intersect_key($large, $small), $small);
To not make the assumption, if there may be keys in either one that aren't in the other then:
$result = array_merge_recursive(array_intersect_key($large, $small),
array_intersect_key($small, $large));
I have the following from my code:
stdClass Object ( [orders] => Array ( [0] => stdClass Object ( [shipping_address] => stdClass Object ( [first_name] => John [last_name] => Doe [company] => [address_1] => 3927 Walnut Grove [address_2] => [city] => Rosemead [state] => CA [postcode] => 90001 [country] => US ) ) [1] => stdClass Object ( [shipping_address] => stdClass Object ( [first_name] => chris [last_name] => koh [company] => [address_1] => 745 bow [address_2] => [city] => diamond [state] => CA [postcode] => 90015[country] => US ) ) ) )
how would i extract just the first_name from both elements?
John and chris
The "Array" you get back is a mishmash of Arrays and Objects, objects are accessed differently.
Array values are accessed by
$an_array = array('apple','banana');
$an_array[0]; //will return apple
While Object values area accessed by
$an_object->key;
Take a look at your return object
stdClass Object ( //<-- Object
[orders] => Array ( //<-- Array
[0] => stdClass Object ( //<-- Object
[shipping_address] => stdClass Object ( //<-- Object
[first_name] => John
etc..
So to get for example the first name you can access it with:
$arrayAndObjects->orders[0]->shipping_address->first_name
Hope that helps you understand it a bit, here are links to the php documentation for Arrays & Objects
http://php.net/manual/en/language.types.object.php
http://php.net/manual/en/language.types.array.php
I have this object:
stdClass Object
(
[daily_inventoryID] => 1
[inventory_timestamp] => 2012-06-08 14:35:42
[inventory_date] => 2012-06-08
[inventory] =>
Array
(
[0] => stdClass Object
(
[ingredientID] => 2
[code] => Bf
[description] => 1st Class Flour
[volume] => 8268
[price] => 750
[amount_gram] => 0.02980
[status] => Inactive
[uom_id] => 1
[flour] => Yes
)
[1] => stdClass Object
(
[ingredientID] => 3
[code] => Sf
[description] => 3rd Class Flour
[volume] => 18490
[price] => 635
[amount_gram] => 0.02540
[status] => Inactive
[uom_id] => 5
[flour] => Yes
)
...........
Let's say, we name the objects as $inv, i would like to display the value of $inv->inventory which is an array containing objects.
How would I do that using foreach or for loop?
foreach ($inv->inventory as $inventory) {
print_r($inventory);
}