This is my response:
Array
(
[0] => Array
(
[1] => Array
(
[aid] => 1
[address] => Surat
[country] => India
[state] => Gujarat
[city] => Surat
[pincode] => 395010
[full_name] => Pra test
[mobile_no] => 7984509142
[email] =>
[default] => 0
)
)
)
I want response like:
Array
(
[1] => Array
(
[aid] => 1
[address] => Surat
[country] => India
[state] => Gujarat
[city] => Surat
[pincode] => 395010
[full_name] => Pra test
[mobile_no] => 7984509142
[email] =>
[default] => 0
)
)
I want to remove 0 index from my response. I want my response like what I define below. so how can I do this with functions and etc..
It sounds like you are trying to remove an extra layer from a multi-dimentional array.
An easy way would be something like:
$new_array = $old_array[0];
Edit:
Per #Gert B's suggestion:
To return the first element regardless of the key:
$new_array = reset($old_array);
The reset function returns the pointer to the beginning of the array, and more importantly for this question, returns the first element of the array.
$result = $old_array[0];
This was the easiest way ...
Try this,
$data = 'YOUR_DATA_ARRAY';
$result = [];
foreach ($data as $category => $attributes) {
foreach ($attributes as $attribute => $options) {
foreach ($options as $option) {
$result[] = array('category' => $category, 'attribute' => $attribute, 'option' => $option);
}
}
}
I use array_replace_recursive() function and get response I want.
now my response is :
Array
(
[1] => Array
(
[aid] => 1
[address] => Surat
[country] => India
[state] => Gujarat
[city] => Surat
[pincode] => 395010
[full_name] => Pra test
[mobile_no] => 7984509142
[email] =>
[default] => 0
)
[2] => Array
(
[aid] => 2
[address] => Surat
[country] => India
[state] => Gujarat
[city] => Surat
[pincode] => 395010
[full_name] => Pra test
[mobile_no] => 7984509142
[email] =>
[default] => 0
)
)
Related
I have a multi dimensional array and i am now sure how to use array chunk on my array key request while preserving the information into the new array. I would like to split the array every 2 arrays. I tried using array_chunk inside of a loop but no luck.
Here is my array.
[0] => Array
(
[first_name] => Richard
[patient_first_name] => Donna
[trip_date] => 2018-08-24
[request] => Array
(
[0] => stdClass Object
(
[id] => 46
[client_id] => 9873
[city] => COOLIDGE
[state] => AZ
[zip] => 85228
)
[1] => stdClass Object
(
[id] => 49
[client_id] => 14965
[city] => CHANDLER
[state] => AZ
[zip] => 85226
)
[2] => stdClass Object
(
[id] => 55
[client_id] => 10120
[city] => PHX
[state] => AZ
[zip] => 85008
)
[3] => stdClass Object
(
[id] => 59
[client_id] => 11229
[city] => BUCKEYE
[state] => AZ
[zip] => 85326
)
[4] => stdClass Object
(
[id] => 69
[client_id] => 13769
[city] => PHOENIX
[state] => AZ
[zip] => 85035
)
[5] => stdClass Object
(
[id] => 175
[client_id] => 16437
[city] => Phx
[state] => Az
[zip] => 85029
)
[6] => stdClass Object
(
[id] => 195
[client_id] => 16457
[city] => Apache Junction
[state] => Az
[zip] => 85120
)
[7] => stdClass Object
(
[id] => 197
[client_id] => 16459
[city] => Mesa
[state] => Az
[zip] => 85204
)
)
)
This is the array I would like.
[0] => Array
(
[first_name] => Richard
[patient_first_name] => Donna
[trip_date] => 2018-08-24
[request] => Array
(
[0] => stdClass Object
(
[id] => 46
[client_id] => 9873
[city] => COOLIDGE
[state] => AZ
[zip] => 85228
)
[1] => stdClass Object
(
[id] => 49
[client_id] => 14965
[city] => CHANDLER
[state] => AZ
[zip] => 85226
)
)
[1] => Array
(
[first_name] => Richard
[patient_first_name] => Donna
[trip_date] => 2018-08-24
[request] => Array
[0] => stdClass Object
(
[id] => 55
[client_id] => 10120
[city] => PHX
[state] => AZ
[zip] => 85008
)
[1] => stdClass Object
(
[id] => 59
[client_id] => 11229
[city] => BUCKEYE
[state] => AZ
[zip] => 85326
)
)
[2] => Array
(
[first_name] => Richard
[patient_first_name] => Donna
[trip_date] => 2018-08-24
[request] => Array
[0] => stdClass Object
(
[id] => 69
[client_id] => 13769
[city] => PHOENIX
[state] => AZ
[zip] => 85035
)
[1] => stdClass Object
(
[id] => 175
[client_id] => 16437
[city] => Phx
[state] => Az
[zip] => 85029
)
)
)
This is my code.
$drivers = [];
foreach($recs as $val => $rec) {
$drivers[$rec->driver_id]['first_name'] = $rec->first_name;
$drivers[$rec->driver_id]['patient_first_name'] = $rec->patient_first_name;
$drivers[$rec->driver_id]['trip_date'] = $rec->trip_date;
$drivers[$rec->driver_id]['request'][] = $rec;
}
foreach($drivers as $val => $driver) {
$drivers = array_chunk($driver['request'], 2);
}
Any suggestions?
Using array-chunk if what you need. Check the following example (I remove some of the data to simplify):
$request = array(["id" => 46], ["id" => 49], ["id" => 55], ["id" => 59], ["id" => 69], ["id" => 175], ["id" => 195], ["id" => 197]);
$arr[] = array("first_name" => "Richard", "request" => $request);
foreach($arr as $driver) {
$requests = array_chunk($driver['request'], 2);
foreach($requests as $chunck) {
$ans[] = array("id" => $driver["first_name"], "request" => $chunck); // here you can add all the other data you need from the "driver" object
}
}
Now , $ans will have your desire output
Get 'request' from the source array, chunk it and add rest items to each element of the result array
$res = array_chunk($recs['request'], 2);
unset($recs['request']);
foreach($res as &$x) {
$x += $recs;
}
I'm trying to check a multidimensional array and keep only the keys they are match and has values (not same value).
So i want to check the whole array and get back the key's they have value, check them in all arrays and only keep the key's that all arrays contains.
So here is my array:
Array
(
[0] => Array
(
[id] => 21
[tstamp] => 1508482179
[firstname] => test1
[lastname] => test1
[dateOfBirth] =>
[gender] =>
[company] =>
[street] =>
[postal] =>
[city] =>
[state] =>
[country] =>
[phone] =>
[mobile] =>
[fax] =>
[email] => test1#test.com
[website] =>
[language] =>
[groups] => a:1:{i:0;s:1:"1";}
[login] => 1
[username] => test2
[assignDir] =>
[homeDir] =>
[disable] =>
[start] =>
[stop] =>
[dateAdded] => 1508482142
[lastLogin] => 0
[currentLogin] => 0
[loginCount] => 3
[locked] => 0
[session] =>
[createdOn] => 0
)
[1] => Array
(
[id] => 2
[tstamp] => 1508482189
[firstname] => test2
[lastname] => test2
[dateOfBirth] =>
[gender] =>
[company] =>
[street] =>
[postal] =>
[city] =>
[state] =>
[country] =>
[phone] =>
[mobile] =>
[fax] =>
[email] => test2#test.com
[website] =>
[language] =>
[groups] => a:1:{i:0;s:1:"1";}
[login] => 1
[username] => test2
[assignDir] =>
[homeDir] =>
[disable] =>
[start] =>
[stop] =>
[dateAdded] => 1508482142
[lastLogin] => 0
[currentLogin] => 0
[loginCount] => 3
[locked] => 0
[session] =>
[createdOn] => 0
)
)
Code:
$arrResultMap = array_map('array_filter', $arrResult);
$currencies = count($arrResultMap) > 1 ? call_user_func_array('array_intersect', $arrResultMap) : array_shift($arrResultMap);
print_r($currencies);
Output:
Array
(
[id] => 1
[groups] => a:1:{i:0;s:1:"1";}
[login] => 1
[loginCount] => 3
)
As you see there are some key's missing. Like email, firstname, lastname, tstamp, etc. I can't see or find what i have done wrong :)
UPDATE:
Found solution instead of using array_intersect i use array_intersect_key.
Now it works.
This code work:
$arrResultMap = array_map('array_filter', $arrResult);
$currencies = count($arrResultMap) > 1 ? call_user_func_array('array_intersect_key', $arrResultMap) : array_shift($arrResultMap);
print_r($currencies);
I've an associative array. I am attempting to create each sub array within a loop and insert it into a multidimensional array. Google suggested using array_merge, but after using 'print_r' on the multidimensional array with the code below, only the last sub-array is being displayed.
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
[id] => 184
[firstName] => skg
[lastName] => s
[address] => Array
(
[address1] => sakthi
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
)
[jobOrder] => Array
(
[id] => 88
[title] => Tech Analyst
)
)
[1] => Array
(
[candidate] => Array
(
[id] => 852
[firstName] => mso cool
[lastName] =>
[address] => Array
(
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
)
[jobOrder] => Array
(
[id] => 57
[title] => Tester
)
)
And Another Array like this :
[rating] => Array
(
[0] => 7
[1] => 5
[2] =>
)
How to get merge this array value into previous array values like
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
[id] => 184
[firstName] => skg
[lastName] => s
[address] => Array
(
[address1] => sakthi
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
[rating] => 7
)
[jobOrder] => Array
(
[id] => 88
[title] => Tech Analyst
)
)
[1] => Array
(
[candidate] => Array
(
[id] => 852
[firstName] => mso cool
[lastName] =>
[address] => Array
(
[address1] =>
[address2] =>
[city] =>
[state] =>
[zip] =>
[countryID] => 1
[countryName] => United States
[countryCode] => US
)
[hourlyRate] => 0
[rating] => 5
)
[jobOrder] => Array
(
[id] => 57
[title] => Tester
)
)
I tried lot of tricks but couldn't get the desired array format. Can any one help me in this to get the desired array? Thanks in advance.
$arr = array(); //filled with data
$rating = array(); //filled with rates
foreach ( $rating as $key =>$rate ) {
$arr['data'][ $key ]['rating'] = $rate;
}
<?php
$arr=Array(
'data'=>Array(
0=>Array('candidate'=>Array()),
1=>Array('candidate'=>Array())
)
);
$rating=Array(7,5);
foreach($rating as $key=>$val){
$arr['data'][$key]['rating']=$val;
}
print_r($arr);
Returns:
Array
(
[data] => Array
(
[0] => Array
(
[candidate] => Array
(
)
[rating] => 7
)
[1] => Array
(
[candidate] => Array
(
)
[rating] => 5
)
)
)
I am using randonuser API to generate dummy images. The API returns JSON that I have used json_decode to decode and using print_r, I got the array below:
Array
(
[results] => Array
(
[0] => Array
(
[user] => Array
(
[gender] => male
[name] => Array
(
[title] => mr
[first] => dwight
[last] => evans
)
[location] => Array
(
[street] => 6822 hunters creek dr
[city] => fresno
[state] => vermont
[zip] => 63409
)
[email] => dwight.evans44#example.com
[username] => ticklishostrich542
[password] => ebony
[salt] => 4xuAIjmh
[md5] => 648f472ff152a194c410d774ff9a4b9d
[sha1] => f23cc7ffd2b8980d10de86bccc85068ecf9b7b45
[sha256] => fec06f7df352a06aab9c30af9d7ab9b5b81dc0bd6b7567b59fba1a731dea6bba
[registered] => 1129218274
[dob] => 409533355
[phone] => (797)-563-6160
[cell] => (200)-718-4014
[SSN] => 213-46-5200
[picture] => Array
(
[large] => http://api.randomuser.me/portraits/men/98.jpg
[medium] => http://api.randomuser.me/portraits/med/men/98.jpg
[thumbnail] => http://api.randomuser.me/portraits/thumb/men/98.jpg
)
[version] => 0.4.1
)
[seed] => cf744a697a08f256
)
. .. .....
and so on.
I just need the large key value under parent picture. How do I loop through it using a foreach statement?
Just access it as you normally would:
$data = json_decode('that json string', true);
foreach($data['results'] as $value) {
echo $value['user']['picture']['large'];
}
Use json_decode($var, true), then you'll have an array, and looping will be easier.
This question is a follow up from this Now for my follow up question I also have this object on page:
Array
(
[registrants] => Array
(
[0] => Registrant Object
(
[title] => D C
[link] => **********
[id] => ***************
[updated] => 2013-03-06T12:11:49-05:00
[lastName] => C
[firstName] => D
[email] => *********
[personalInformation] => PersonalInformation Object
(
[cellPhone] =>
[label] =>
[addr1] =>
[addr2] =>
[addr3] =>
[city] =>
[state] =>
[postalCode] =>
[province] =>
[country] =>
[phone] =>
)
[businessInformation] => BusinessInformation Object
(
[fax] =>
[website] =>
[blog] =>
[company] =>
[jobTitle] =>
[department] =>
[label] =>
[addr1] =>
[addr2] =>
[addr3] =>
[city] =>
[state] =>
[postalCode] =>
[province] =>
[country] =>
[phone] =>
)
[customInformation1] => Array
(
)
[customInformation2] => Array
(
)
[registrationStatus] => REGISTERED
[registrationDate] => 2013-03-06T12:11:49-05:00
[guestCount] => 0
[paymentStatus] => NA
[orderAmount] =>
[currencyType] =>
[paymentType] =>
[costs] => Array
(
)
)
[1] => Registrant Object
(
[title] => Test Test
[link] => ****
[id] => *************
[updated] => 2013-03-06T12:47:47-05:00
[lastName] => Test
[firstName] => Test
[email] => ***************
[personalInformation] => PersonalInformation Object
(
[cellPhone] =>
[label] =>
[addr1] =>
[addr2] =>
[addr3] =>
[city] =>
[state] =>
[postalCode] =>
[province] =>
[country] =>
[phone] =>
)
[businessInformation] => BusinessInformation Object
(
[fax] =>
[website] =>
[blog] =>
[company] =>
[jobTitle] =>
[department] =>
[label] =>
[addr1] =>
[addr2] =>
[addr3] =>
[city] =>
[state] =>
[postalCode] =>
[province] =>
[country] =>
[phone] =>
)
[customInformation1] => Array
(
)
[customInformation2] => Array
(
)
[registrationStatus] => REGISTERED
[registrationDate] => 2013-03-06T12:47:47-05:00
[guestCount] => 0
[paymentStatus] => NA
[orderAmount] =>
[currencyType] =>
[paymentType] =>
[costs] => Array
(
)
)
)
[nextLink] =>
)
So following the same theory I am retriving the values like this:
<?php echo $Registrant->lastName; echo $Registrant->firstName; echo $Registrant->email; ?>
but this only retrieves the first lastname and firstname from [0] => Registrant Object not from 1 => Registrant Object how to i get all of the first names and last names?
Thank everyone for there interest and there time.
Kind regards
Chris
To explain your scenario a bit further than other answers.
You have an array of (Registrant) objects here. This is actually an associative array (as all PHP arrays) with indices from 0 - 1.
$registrantObjects[0] // would give first Registrant object
$registrantObjects[1] // would give second Registrant object
You can access them both. But if you want to iterate the array (i.e. going over all elements and do the same for each one), you should use a loop.
PHP has a nice foreach loop for this use case:
foreach ($registrantObjects as $registrant) {
// $registrant is a Registrant object here
echo $registrant->lastName;
}
You could also try this:
foreach ($registrantObjects as $index => $registrant) {
// $registrant is a also Registrant object here
// But we have a variable $index, too. It represents the current 'key'
// We have a normal (numbered) array thus the keys are [0..1]
echo $registrant->lastName;
}
And both loops are equal to this for loop:
for ($i = 0, $len = count($registrantObjects); $i < $len; $i++) {
// $registrantObjects[$i] gives a Registrant object
}
Lets say the object you printed is "$RegistrantObjects"
You can do the following thing :
foreach ($RegistrantObjects as $registrant)
{
echo $registrant->lastName;
}
Within the foreach, the $registrant object will be accessible the same way that you code accesses it.
Try the below code;
<?php
foreach($Registrant as $reg) {
echo $reg->firstname;
echo $reg->lastname;
}
?>