I am using cakephp 1.3 to extract data from a table with Cities and Postal Codes and I would like to extract these two columns to populate drop down menu with postal codes as menu 'id' and town names as menu 'values'.
The “find( 'list')” method is out of option because some of the towns have the same postcodes and the class is picking only unique value so instead of getting :
Array
(
[800] => Darwin
[801] => Darwin
[810] => Allawa
)
I am getting
Array
(
[800] => Darwin
[810] => Allawa
)
The method find('all') is returning a multidimensional array
Array
(
[0] => Array
(
[GeoPostcode] => Array
(
[state_id] => 26
[region] => Acton
)
)
[1] => Array
(
[GeoPostcode] => Array
(
[state_id] => 26
[region] => Ainslie
)
)
)
What is the best work around to get the desired data in unidimensional array without reducing the output to unique array keys? Something like :
Array ( [800] => Darwin [801] => Darwin [810] => Allawa )
You should still be able to do this with $Model->find('list').
$this->Model->find(
'list',
array(
'fields' => array(
'postal_code',
'city_name'
),
'contain' => false
)
);
This should return a list resultset using the two fields that you selected (postal code and city name).
Your alternate option, is to do a find('all'):
$results = $this->Model->find(
'all',
array(
'conditions' => array( ... )
)
);
Now, you have this format in your data results:
$results = Array
(
[0] => Array
(
[ModelName] => Array
(
[id] => 1
[postal_code] => 800,
[city_name] => 'Darwin'
)
)
[1] => Array
(
[ModelName] => Array
(
[id] => 2
[postal_code] => 801,
[city_name] => 'Darwin'
)
)
[2] => Array
(
[ModelName] => Array
(
[id] => 3
[postal_code] => 810,
[city_name] => 'Allawa'
)
)
)
Then use the Set class within Cake to extract some data:
$keys = Set::extract('/ModelName/postal_code', $results);
$values = Set::extract('/ModelName/city_name', $results);
/* Then some array_combine magic */
$list = array_combine($keys, $values);
Now you have an array that is what you want. Albeit a longer way, but it works. You can then pass this variable to your Form::select() helper to create your dropdown.
Hope this helps!
Related
I have a a Multidimensional array with a lot of content that includes a label for each piece of content, and I have a separate array with a Key that is the label being searched for and the value that is to replace it.
I want to use the second array to replace nested label values in the first array. The function should find the first Array's label value based on the key of the second array, and replace it using that Key's value.
For Example the content Array:
Array
(
[Name] => Array
(
[0] => Array
(
[label] => Name
[content] => Important Paper
)
[1] => Array
(
[label] => Item Type
[content] => Document
)
[2] => Array
(
[label] => Author
[content] => Bob Jones
)
)
[date] => Array
(
[0] => Array
(
[label] => Date
[content] => 2009
)
)
)
And an example of the array looking to replace the value
Array
(
[Name] => Record
[Author] => Researcher
[Date] => Year
)
The output I would want from this function would resemble
Array
(
[Name] => Array
(
[0] => Array
(
[label] => Record
[content] => Important Paper
)
[1] => Array
(
[label] => Item Type
[content] => Document
)
[2] => Array
(
[label] => Researcher
[content] => Bob Jones
)
)
[date] => Array
(
[0] => Array
(
[label] => Year
[content] => 2009
)
)
)
Right now I am attempting to get the results by using a series of forloops and if statements. I don't actually know the depth that the [label] key will be found at, and it should only update content with the key value [label].
Any insight on a way to do this more efficiently would be great. I've also been looking at all of the different php array functions and trying to find the best combinations.
you can use: array_walk_recursive
from:
php.net
$orig = array('Name' => [['label' => 'Name', 'content' => 'Important Paper'],
['label' => 'Item Type', 'content' => 'Document'],
['label' => 'Author', 'content' => 'Bob Jones']],
'date' => [[['label' => 'Date', 'content' => '2009']]]);
// notice the `&` symbol, this allows us to change the original array on the fly
function replaceIt(&$item){
$replaceArray = ['Name' => 'Record',
'Author' => 'Researcher',
'Date' => 'Year'];
foreach ($replaceArray as $key => $value) {
if($key == $item){ $item = $value; }
}
}
array_walk_recursive($orig, 'replaceIt');
print('<pre>');
print_r($orig);
print('</pre>');
im trying to filter a JSON array response as i only need a small part of the results.
I need to get the players displayName only.
Here is the repose for the first player, there can be upto 12 player per match.
I need something that can loop through and extract the names..
[displayName] => jonhofun
At present the only way i can get the data i need is by doing
$player1 = $json11['Response']['data']['entries']['0']['player']['destinyUserInfo']['displayName'];
$player2 = $json11['Response']['data']['entries']['1']['player']['destinyUserInfo']['displayName'];
etc... etc...
heres the original response
Array
(
[Response] => Array
(
[data] => Array
(
[period] => 2016-08-20T10:16:46Z
[activityDetails] => Array
(
[referenceId] => 3156370656
[instanceId] => 5370359303
[mode] => 12
[activityTypeHashOverride] => 3614615911
)
[entries] => Array
(
[0] => Array
(
[standing] => 0
[score] => Array
(
[basic] => Array
(
[value] => 2190
[displayValue] => 2,190
)
)
[player] => Array
(
[destinyUserInfo] => Array
(
[iconPath] => /common/destiny_content/icons/d0d3cd4c26aa1a931d46c4bf720856ba.jpg
[membershipType] => 2
[membershipId] => 4611686018454971653
[displayName] => jonhofun
)
[characterClass] => Warlock
[characterLevel] => 40
[lightLevel] => 322
)
)
)
)
)
)
Any help would be appreciated.
You need to loop through the sub array under "entries".
foreach ($json11['Response']['data']['entries'] as $entries) {
$player_names[] = $entries['player']['destinyUserInfo']['displayname'];
}
echo "<pre>";
print_r($player_names); // Check all player names
Can you please explain how to change one array value to second value.
Please find below example.
First array
Array
(
[20239802] => one test
[20239801] => two testttttt
)
Second array
Array (
[content] => Array (
[0] => Array (
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239802
[track] => Lede Inthati Santhosham
[duration] => 0
)
[1] => Array (
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239801
[track] => Lede Inthati Santhosham
[duration] => 0
)
)
[autoshuffle_pack] => no
)
We need to replace [track] value in second array if match first array [20239802] with second array [content_id]
Need out put:-
Array
(
[content] => Array
(
[0] => Array
(
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239802
[track] => one test
[duration] => 0
)
[1] => Array
(
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239801
[track] => two testttttt
[duration] => 0
)
)
[autoshuffle_pack] => no
)
Check [track] value change in my need out put
as per depend first array.
Array
(
[20239802] => one test
[20239801] => two testttttt
)
with second array
[content_id] => 20239801
[track] => two testttttt
Is this you want to do :
foreach($second_array as $key => $second_row) {
$content_id = $second_row['content_id'];
if(isset($first_array[$content_id]) && $first_array[$content_id] != '') {
$second_array['track'] = $first_array[$content_id];
}
}
You can do you own function.
Foreach records in your first array:
Loop though the second and search for key == content_id
If it matched, set the value of array2[$index][track] to array1[key]
If not just continue
You can also use array_search() function to find the match faster, for I'll let you take a look at the PHP documentation
I am experimenting with my first API and getting stuck with the results. I am getting an Array Back:
Array
(
[GetOrderListResult] => Array
(
[Status] => Success
[MessageCode] => 0
[ResultData] => Array
(
[OrderResponseItem] => Array
(
[0] => Array
(
[NumberOfMatches] => 2
[OrderTimeGMT] => 2014-05-05T03:23:00
[LastUpdateDate] => 2014-05-28T11:41:45.953
[TotalOrderAmount] => 12.7800
[OrderState] => Active
[DateCancelledGMT] =>
[OrderID] => 138711
[ClientOrderIdentifier] => 138711
[SellerOrderID] =>
[OrderStatus] => Array
(
[CheckoutStatus] => NotVisited
[CheckoutDateGMT] => 1900-01-01T00:00:00
[PaymentStatus] => NotSubmitted
[PaymentDateGMT] => 1900-01-01T00:00:00
[ShippingStatus] => Unshipped
[ShippingDateGMT] => 1900-01-01T00:00:00
[OrderRefundStatus] => NoRefunds
)
)
[1] => Array
(
[NumberOfMatches] => 2
[OrderTimeGMT] => 2014-05-05T03:23:00
[LastUpdateDate] => 2014-05-28T12:59:01.78
[TotalOrderAmount] => 6.3900
[OrderState] => Active
[DateCancelledGMT] =>
[OrderID] => 138750
[ClientOrderIdentifier] => 138750
[SellerOrderID] =>
[OrderStatus] => Array
(
[CheckoutStatus] => NotVisited
[CheckoutDateGMT] => 1900-01-01T00:00:00
[PaymentStatus] => NotSubmitted
[PaymentDateGMT] => 1900-01-01T00:00:00
[ShippingStatus] => Unshipped
[ShippingDateGMT] => 1900-01-01T00:00:00
[OrderRefundStatus] => NoRefunds
)
)
)
)
)
)
Now the onyl way I know how to reference a fied such as the order id in the array is:
echo "Order ID: ".$result['GetOrderListResult']['ResultData']['OrderResponseItem']['0']['OrderID'];
But I want to be able to loop through the array of orders and execute code for each item, could somewbody please point me in the right direction for:
a) is there a better way to refernce these fields?
b) how do I loop through the OrderResponseItem part of the array?
The only loop I could think of was for the whole array not nested items in the array.
Sorry if I'm missing something simple....
Thanks and if you need the data in any other format please let me know.
Since you already know the keys you could just use a foreach to access them an point to that key then loop. Something like this:
foreach($result['GetOrderListResult']['ResultData']['OrderResponseItem'] as $value) {
$order_id = $value['OrderID'];
// your other processes
}
hi i have the follwing array structure
Array
(
[_id] => MongoId Object
(
[$id] => 538978ce8ead0ec1048b456c
)
[cartId] => 98374319ff71dbc3a84b842b7a443cf7
[products] => Array
(
[0] => Array
(
[productId] => 100343
[quantity] => 17
[name] => a
)
[1] => Array
(
[productId] => 100344
[quantity] => 3
[name] => ab
)
[2] => Array
(
[productId] => 100345
[quantity] => 1
[name] => abc
)
)
And i'm having problems to increment the quantity of the products based on the productId
I now use the position but i have no reference on id
$oCartsCollection->update(array('cartId'=>'98374319ff71dbc3a84b842b7a443cf7'), array('$inc' => array('products.0.quantity'=>1)));
What you need to do is add to your query to select and element from your array and then use the positional $ operator in order to match that position:
$oCartsCollection->update(
array(
'cartId'=>'98374319ff71dbc3a84b842b7a443cf7',
'products.productId' => 100343
),
array('$inc' => array('products.$.quantity'=>1)));
The "dot" notation method is fine for accessing the productId element in this case. For multiple fields to match use $elemMatch instead
$oCartsCollection->update(
array(
'cartId'=>'98374319ff71dbc3a84b842b7a443cf7',
'products' => array(
'$elemMatch' => array(
'productId' => 100343,
'name' => 'a'
)
)
),
array('$inc' => array('products.$.quantity'=>1)));