I have the following array:
array(2) {
[0] => array(3) {
["submission_id"] => int(28)
["date"] => string(22) "2010-10-18 15:55:33+02"
["user_id"] => int(12)
}
[1] => array(3) {
["submission_id"] => int(37)
["date"] => string(22) "2010-11-21 16:02:07+01"
["user_id"] => int(23)
}
I want to get only the user_id key values from this array. I could obviously loop over it, but I was wondering if there was a quicker way.
You could use array_map (might not be quicker though, as it will make a function call per array element):
function getUserId($a) {
return $a['user_id'];
}
$user_ids = array_map('getUserId', $array);
Apart from that, looping is the only way (array_map makes loop anyway).
You can access only the user_id values like this if you know the array index you want to access:
$arr = your array here..
echo $arr[0]['user_id'];
echo $arr[1]['user_id'];
Related
I'm studying php and I'm trying to figure out how to get the string "Juve_Milan" from this var_dump($_POST) :
array(5) {
["Juve_Milan"] => array(2) {
[0] => string(1)
"5" [1] => string(1)
"1"
}["Inter_Roma"] => array(2) {
[0] => string(1)
"4" [1] => string(1)
"4"
}["Napoli_Lazio"] => array(2) {
[0] => string(1)
"2" [1] => string(1)
"5"
}["name"] => string(0)
"" ["submit"] => string(5)
"Invia"
}
I could get all of them with:
foreach ($_POST as $param_name => $param_val) {
echo "<tr><td>".$param_name."</td><td>".$param_val[0]."-".$param_val[1]."</td></tr>";
}
But i want to get them exactly one by one, for example, if i want to get the string "Juve_Milan" or "inter_Roma" how can i do?
Without looping, how can i get the string value: "Juve_milan" or "Inter_Roma"? Becouse with the loop i can access them this way : $_POST as $param_name => $param_val
But i want to get them without loop, my first attempt was something like $_POST[0][0] but its wrong...
There are numbers of php array functions you can use.
You can use
array shift
to remove an element from an array and display it .e.g
$club = ['juve_millan', 'inter_roma', 'napoli_lazio'];
$juve = array_shift($club);
echo $juve;// 'juve_millan'
but note that the array is shortened by one element i.e. 'juve_millan' is no more in the array and also note that using
array_shift
over larger array is fairly slow
Array Slice function
PHP
array_slice()
function is used to extract a slice of an array e.g
$club = ['juve_millan', 'inter_roma', 'napoli_lazio'];
if I want to display
inter_roma
or assigned it to a variable, then I can do this
$roma = array_slice($club, 1, 1);// The first parameter is the array to slice, the second parameter is where to begin(which is one since most programming language array index start from 0 meaning juve_millan is zero, while inter_roma is 1, napoli_lazio is 2) and the length is 1 since i want to return a single element.
I hope you understand
You could iterate over keys of the main array like this:
foreach($_POST as $param_name => $param_val) {
echo "<tr><td>".$param_name."</td></tr>";
}
This will return each Juve_Milan, Inter_Roma etc. one by one. Although it's part of the code you already have, I believe this will return values you want only.
var_dump($response);
outputs:
array(1) { ["metafields"]=> array(1) { [13]=> array(10) { ["id"]=> int(32616923206) ["namespace"]=> string(7) "ly26638" } } }
array(1) { ["metafields"]=> array(1) { [13]=> array(10) { ["id"]=> int(32641864774) ["namespace"]=> string(7) "ly26638" } } }
how can I convert $response to an object to operate it like in the following code:
echo $response->metafields[0]->id;
I've tried the code below, but without any result :/
$object = json_decode(json_encode($response));
If you want to access your "metafields's id" like $response->metafields[13][id] then you need to cast your response array to object.
Ex.
$response = (object) array(
'metafields' => array(
'13' => (object) array(
'id' => 32616923206,
'namespace' => "ly26638"
)
)
);
Then you can use syntax like $response->metafields[13]->id to access the "id" value.
If you make an object with (object) $response, the child elements will still be arrays. Then you could do
$response->metafields[13][id]
If you want the object notation through the whole chain you need something like the function sandip has provided.
But why not just make this call:
$response[metafields][13][id]
You can't find the member of an object by '[]'.
The '[]' is for arrays, and the '->' is for objects.
So the reason for the blank output is: metafields[0].
The code below is wrong:
echo $response->metafields[0]->id;
So you should change the key of the array, the key of an array should not be a pure number.
I tried to change the key to 'a13', and with the code of:
echo $response->metafields->a13->id;
I got "32616923206".
That's all. O(∩_∩)O~
I want to store 2 dependent values in this array:
["STEP5"]=> array(1) {
["OPTIONS"]=>
array(2) {
[0]=>
string(4) "opt2"
[1]=>
string(4) "opt3"
} }
Option fields (opt1, opt2,...) can have an extra field input. So how to store that in my array ? Thanx a lot.
You make each element of the OPTIONS array into an array itself. This would be better illustrated as:
["STEP5"] => array(1) {
["OPTIONS"] => array(2) {
[0] => array(2) {
["TEXT"] => "opt2"
["INPUT"] => "input data"
}
[1] => array(1) {
["TEXT"] => "opt3"
// No input data here
}
}
}
Then you can access the fields as:
$foo["STEP5"]["OPTIONS"][0]["TEXT"]
$foo["STEP5"]["OPTIONS"][0]["INPUT"]
$foo["STEP5"]["OPTIONS"][1]["TEXT"]
You don't have to have the "INPUT" field for every single option, but you can add it if you need to. Also, you might consider using objects to accomplish this task as they provide lots of useful functionality.
I have this example of an array of objects and I need to randomize it each time I do a foreach loop.
Looks like shuffle works only on arrays.
One thing is that I cant convert this into an array, because then it will become a STD object which I can't use, because of the way my mapper works.
array(48) {
[0] => object(Friends_Model_Friends)#142 (4) {
["_id":protected] => NULL
["_tal":protected] => object(stdClass)#194 (3) {
["thumb"] => object(stdClass)#196 (6) {
["id"] => string(8) "10884697"
["type"] => string(1) "2"
["gallery"] => string(1) "1"
}
}
["_friend":protected] => string(7) "3492149"
["_dependent_table":protected] => NULL
}
[1] => object(Friends_Model_Friends)#195 (4) {
["_id":protected] => NULL
["_tal":protected] => object(stdClass)#143 (3) {
["thumb"] => object(stdClass)#202 (6) {
["id"] => string(8) "11160632"
["type"] => string(1) "2"
["gallery"] => string(1) "1"
}
}
["_friend":protected] => string(7) "3301541"
["_dependent_table":protected] => NULL
}
....
Any ideas on shuffling this?
edit: Zend_Debug::dump(shuffle($object)); returns bool(true)
<?php
$my_array;
echo '<pre>'; print_r($my_array); echo '</pre>';
shuffle ($my_array);
echo '<pre>'; print_r($my_array); echo '</pre>';
?>
Try this code
But the objects are in an array, not another object so you can use shuffle...
Shuffle always returns a boolean:
bool shuffle ( array &$array )
So you can't return it like
return shuffle( $myAwesomeArray );
This is what I just did and I wondered why it didn't give anything back, as you're probably expecting your shuffled array back.
You just have to call it like this
shuffle( $myAwesomeArray );
return $myAwesomeArray;
And all should work just fine.
I would like to push an associate array into another array but I an not sure how to go about it. At the minute I have the following:
$rate_info = array(
"hotel_rating" => $hotel->{'hotelRating'},
"room_rate" => $hotel->{'RoomRateDetailsList'}->{'RoomRateDetails'}->{'RateInfo'}->{'ChargeableRateInfo'}->{'#total'},
"currency" => $hotel->{'RoomRateDetailsList'}->{'RoomRateDetails'}->{'RateInfo'}->{'ChargeableRateInfo'}->{'#currencyCode'},
"deep_link" => $hotel->{'deepLink'}
);
array_push($hotel_array[$hotel->{'name'}]["offers"], "expedia" => $rate_info );
"Offers" is an array , all I want to do is add an key value with an array within in. Any ideas? All I seem to be getting at the minute is parse errors.
UPDATE
This is the output of the array so far
["offers"]=>
array(2) {
["LateRooms"]=>
array(4) {
["hotel_rating"]=>
int(4)
["room_rate"]=>
string(6) "225.06"
["currency"]=>
string(3) "USD"
}
[0]=>
string(4) "test"
}
As you can see instad of [0] I would like ["site"]=>array()
Thanks
Oliver
I'd do this for the array assignment:
$hotel_array[$hotel->name]['offers']['expedia'] = $rate_info;
Ensure your warnings are enabled, so you know arrays (and subarrays) have been set up before you use them.
Did you first do this?
$hotel_array[$hotel->{'name'}] = array();
And then you can do:
array_push($hotel_array[$hotel->{'name'}]["offers"], "expedia" => $rate_info );