php array search key & value by value form other array - php

I have an reference array like this (the data amount is dynamic, about 3000 records)
Array
(
[0] => Array
(
[DEPT] => FIN
[KEYCODE] => AAAAA
[TYPE] => 7
)
[1] => Array
(
[DEPT] => SALE
[KEYCODE] => BBBBB
[TYPE] => 16
)
[2] => Array
(
[DEPT] => SCM
[KEYCODE] => CCCCC
[TYPE] => 1
)
)
and use the value of "TYPE" to search keys "[9][7][16][1]" and value of "KEYCODE" to search value of above keys in following array. (dynamic, about 5000 records)
Array
(
[0] => Array
(
[NAME] => Nick
[9] => GGGGG
[7] => AAAAA
[16] => MMMMM
[1] => KKKKK
)
[1] => Array
(
[NAME] => Chris
[9] => PPPPP
[7] => BBBBB
[16] => ZZZZZ
[1] => RRRRR
)
[2] => Array
(
[NAME] => Cathy
[9] => SSSSS
[7] => UUUUU
[16] => JJJJJ
[1] => AAAAA
)
[3] => Array
(
[NAME] => Allen
[9] => FFFFF
[7] => DDDDD
[16] => WWWWW
[1] => CCCCC
)
)
the output array should be
Array
(
[0] => Array
(
[NAME] => Chris
[9] => PPPPP
[7] => HHHHH
[16] => ZZZZZ
[1] => RRRRR
)
[1] => Array
(
[NAME] => Cathy
[9] => SSSSS
[7] => UUUUU
[16] => JJJJJ
[1] => XXXXX
)
)
"Nick" have item [7]=> AAAAA and "Allen" have item [1]=> CCCCC, so delete from data array.
I know the way to delete array item, but no idea to implement compare and search by key/value pair from other array.

For what I understand from your question, here's a simple solution for your required task:
$firstArr = array(
0 => array(
"DEPT" => 'FIN',
'KEYCODE' => 'AAAAA',
'TYPE' => 7
),
1 => array(
'DEPT' => 'SALE',
'KEYCODE' => 'BBBBB',
'TYPE' => 16
),
2 => array(
'DEPT' => 'SCM',
'KEYCODE' => 'CCCCC',
'TYPE' => 1
)
);
$secondArr = array(
0 => array(
'NAME' => 'Nick',
9 => 'GGGGG',
7 => 'AAAAA',
16 => 'MMMMM',
1 => 'KKKKK',
),
1 => array(
'NAME' => 'Chris',
9 => 'PPPPP',
7 => 'HHHHH',
16 => 'ZZZZZ',
1 => 'RRRRR'
),
2 => array(
'NAME' => 'Cathy',
9 => 'SSSSS',
7 => 'UUUUU',
16 => 'JJJJJ',
1 => 'XXXXX'
),
3 => array(
'NAME' => 'Allen',
9 => 'FFFFF',
7 => 'DDDDD',
16 => 'WWWWW',
1 => 'CCCCC'
)
);
foreach($firstArr as $row){
foreach($secondArr as $i => $que){
if(array_key_exists($row['TYPE'], $que) && $row['KEYCODE'] == $que[$row['TYPE']]){
unset($secondArr[$i]); // deleting selected item from the array...
}
}
}
echo '<pre>';
print_r($secondArr);
echo '</pre>';
Output of the above code:
Array
(
[1] => Array
(
[NAME] => Chris
[9] => PPPPP
[7] => HHHHH
[16] => ZZZZZ
[1] => RRRRR
)
[2] => Array
(
[NAME] => Cathy
[9] => SSSSS
[7] => UUUUU
[16] => JJJJJ
[1] => XXXXX
)
)

Related

Array format php

I need to change the format of an array returned in CodeIgniter, leaving a field of the database as index, I made the same querys in PHP and CodeIgniter and both are different, any suggestions? I am using result_array () in CodeIgniter and also result ();
I need this:
Array
(
[1_1] => Array
(
[0] => 1_1
[1] => 16
[2] => ch
[3] => Chemistry
)
[1_2] => Array
(
[0] => 1_2
[1] => 17
[2] => ch
[3] => Chemistry
)
[2_3] => Array
(
[0] => 2_3
[1] => 18
[2] => ch
[3] => Chemistry
)
[2_5] => Array
(
[0] => 2_5
[1] => 19
[2] => ch
[3] => Chemistry
)
[9_1] => Array
(
[0] => 9_1
[1] => 20
[2] => ch
[3] => Chemistry
)
)
Query results:
Array
(
[0] => Array
(
[pos] => 1_1
[tbl_id] => 16
[sub_id] => ch
[sub_name] => Chemistry
)
[1] => Array
(
[pos] => 1_2
[tbl_id] => 17
[sub_id] => ch
[sub_name] => Chemistry
)
[2] => Array
(
[pos] => 2_3
[tbl_id] => 18
[sub_id] => ch
[sub_name] => Chemistry
)
[3] => Array
(
[pos] => 2_5
[tbl_id] => 19
[sub_id] => ch
[sub_name] => Chemistry
)
[4] => Array
(
[pos] => 9_1
[tbl_id] => 20
[sub_id] => ch
[sub_name] => Chemistry
)
)
How can I delete the array that is at the beginning? Thank you.
$array = [
[
'pos' => '1_1',
'tbl_id' => 16,
'sub_id' => 'ch',
'sub_name' => 'Chemistry'
]
];
$result = [];
foreach ($array as $data) {
$values = array_values($data);
$result[$data['pos']] = $values;
}
This will give you what you want.
Array
(
[1_1] => Array
(
[0] => 1_1
[1] => 16
[2] => ch
[3] => Chemistry
)
)

PHP5.3 json_encode is outputting bad json

I am attempting to json_encode an array in php5.3.
json_encode($paperwork_info[0])
The result is bad json. The array is as follows:
[paperwork_guid] => c5bfe512-908d-c513-5a5e-e3a2fbb5548b
[name] => recycle
[sections] => Array
(
[0] => header
[1] => customer
[2] => scope_bullets
[3] => product
[4] => signature
[5] => installer
[6] => order_data
)
[data] => Array
(
[header] => Array
(
[image] => Array
(
[src] => recycle.png
[format] => float:left
)
)
[customer] => Array
(
[f_name] => TEST ONLY
[l_name] => NEIMEIER
[middle_name] => none
[title] => none
[address1] => 28 OEHMAN BLVD
[address2] =>
[address3] =>
[zip] => 14225
[city] => BUFFALO
[state_abbrev] => NY
[email_address] => DALE.NEIMEIER#INSTALLS.COM
[phones] => Array
(
[alt] =>
[cell] =>
[work] =>
[home] => 7165551212
)
)
[scope_bullets] => Array
(
[sku_3001] => Array
(
[bullet_0] => Array
(
[paperwork_bullet_obj_guid] => 2ebefa96-6f6b-069e-e194-245d138b9845
[ordering] => 1
[bullet_text] => This is bullet point #1
[child_level] => 1
[formatting] => Array
(
[0] => Array
(
[start_word] => 2
[num_words] => 0
[tag] => b
[href] =>
)
[1] => Array
(
[start_word] => 2
[num_words] => 1
[tag] => a
[href] => http://www.cnn.com
)
)
)
[bullet_1] => Array
(
[paperwork_bullet_obj_guid] => 734db3f4-01a0-b025-9624-cc52a1845dff
[ordering] => 1
[bullet_text] => Sub-point #1.1
[child_level] => 2
)
[bullet_2] => Array
(
[paperwork_bullet_obj_guid] => ebf5ef02-906e-2005-e499-27eae2edefe9
[ordering] => 1
[bullet_text] => Sub point #1.1.1
[child_level] => 3
)
[bullet_3] => Array
(
[paperwork_bullet_obj_guid] => 447997c1-fd9c-25be-b9bf-39257009fb8b
[ordering] => 1
[bullet_text] => This is bullet point #2
[child_level] => 1
)
[bullet_4] => Array
(
[paperwork_bullet_obj_guid] => 5def2d9c-d322-788c-0afe-d13707004b97
[ordering] => 1
[bullet_text] => Sub point #2.1
[child_level] => 2
)
[bullet_5] => Array
(
[paperwork_bullet_obj_guid] => 84936151-65a3-bcca-951f-b69ff16d34ec
[ordering] => 2
[bullet_text] => Sub point #2.2
[child_level] => 2
)
)
[sku_4405] =>
)
[product] => Array
(
[0] => Array
(
[model] => Paperwork Test abc123
[serial] => 777333
[weight] => 26
[size] =>
[width] =>
[height] => 32
[num_boxes] => 1
[product_name] =>
[sku] =>
[model_number] =>
[part_number] =>
[serial_number] =>
[cat_value] => Best Buy Equipment
[product] =>
[product_type] =>
[product_size] =>
[category] =>
)
)
[signature] => Array
(
[date] => 2016-06-15
[need_customer] => 1
[need_tech] => 1
[need_store] =>
[need_warehouse] =>
)
[installer] => Array
(
[anum] => 45455
[inst_obj_guid] => fb91cf85-381c-b740-e063-775151743da2
[phone] => (317) 519-0481
[tech_f_name] =>
[tech_l_name] =>
[company_name] => ZICO LLC
[email_address] => ZICOLLC#GMAIL.COM
)
[order_data] => Array
(
[assigned_date] => 2016-06-07 22:24:47-04
[purchased_date] => 2016-06-06
[creation_date] => 2016-06-06
[install_date] => 2016-06-10
[install_time] => 19:00:00
[prescheduled_date] => 1969-12-31
[prom_start_time] => 2016-06-10 08:06:00
[prom_end_time] => 2016-06-10 11:06:00
[client_order_id] => 12365478996325412
[job_num] => 4043269
[campaign_wkord_obj_guid] => a9a8cc0b-d7ef-ac0e-df61-dad41c023cb0
[pos_obj_guid] => 096e38c2-55c3-80cf-0778-23f81f1cf2f6
[inst_obj_guid] => fb91cf85-381c-b740-e063-775151743da2
[client_obj_guid] => 247e893a-3ea4-c544-47b2-f23ff16017c6
[ord_obj_guid] => ae55e034-d7d0-5d13-3dd3-22f99df8ead4
[skus] => Array
(
[0] => Array
(
[job_type_obj_guid] => 8234ca2c-e40e-c48c-befc-7ceac5e9de32
[job_sku] => 3001
[client_sku] =>
[sku_name] => Site Survey
)
[1] => Array
(
[job_type_obj_guid] => a3f60c3b-ad3c-d828-9898-fa200edcd3cd
[job_sku] => 4405
[client_sku] =>
[sku_name] => Home Delivery
)
)
[client_specific_data] =>
)
)
)
The resultant json string is as follows:
{"paperwork_guid":"c5bfe512-908d-c513-5a5e-e3a2fbb5548b","name":"recycle","sections":{"0":"header","1"
:"customer","2":"scope_bullets","3":"product","4":"signature","5":"installer","6":"order_data"},"data"
:{"header":{"image":{"src":"recycle.png","format":"float:left"}},"customer":{"f_name":"TEST ONLY","l_name"
:"NEIMEIER","middle_name":"none","title":"none","address1":"28 OEHMAN BLVD","address2":"","address3"
:null,"zip":"14225","city":"BUFFALO","state_abbrev":"NY","email_address":"DALE.NEIMEIER#INSTALLS.COM"
,"phones":{"alt":"","cell":"","work":"","home":"7165551212"}},"scope_bullets":{"sku_3001":{"bullet_0"
:{"paperwork_bullet_obj_guid":"2ebefa96-6f6b-069e-e194-245d138b9845","ordering":"1","bullet_text":"This
is bullet point #1","child_level":"1","formatting":{"0":{"start_word":"2","num_words":"0","tag":"b"
,"href":""},"1":{"start_word":"2","num_words":"1","tag":"a","href":"http:\/\/www.cnn.com"}}},"bullet_1"
:{"paperwork_bullet_obj_guid":"734db3f4-01a0-b025-9624-cc52a1845dff","ordering":"1","bullet_text":"Sub-point
#1.1","child_level":"2"},"bullet_2":{"paperwork_bullet_obj_guid":"ebf5ef02-906e-2005-e499-27eae2edefe9"
,"ordering":"1","bullet_text":"Sub point #1.1.1","child_level":"3"},"bullet_3":{"paperwork_bullet_obj_guid"
:"447997c1-fd9c-25be-b9bf-39257009fb8b","ordering":"1","bullet_text":"This is bullet point #2","child_level"
:"1"},"bullet_4":{"paperwork_bullet_obj_guid":"5def2d9c-d322-788c-0afe-d13707004b97","ordering":"1","bullet_text"
:"Sub point #2.1","child_level":"2"},"bullet_5":{"paperwork_bullet_obj_guid":"84936151-65a3-bcca-951f-b69ff16d34ec"
,"ordering":"2","bullet_text":"Sub point #2.2","child_level":"2"}},"sku_4405":null},"product":{"0":{"model"
:"Paperwork Test abc123","serial":"777333","weight":"26","size":null,"width":null,"height":"32","num_boxes"
:"1","product_name":null,"sku":null,"model_number":null,"part_number":null,"serial_number":null,"cat_value"
:"Best Buy Equipment","product":null,"product_type":null,"product_size":null,"category":null}},"signature"
:{"date":"2016-06-15","need_customer":true,"need_tech":true,"need_store":false,"need_warehouse":false
},"installer":{"anum":"45455","inst_obj_guid":"fb91cf85-381c-b740-e063-775151743da2","phone":"(317) 519-0481"
,"tech_f_name":null,"tech_l_name":null,"company_name":"ZICO LLC","email_address":"ZICOLLC#GMAIL.COM"
},"order_data":{"assigned_date":"2016-06-07 22:24:47-04","purchased_date":"2016-06-06","creation_date"
:"2016-06-06","install_date":"2016-06-10","install_time":"19:00:00","prescheduled_date":"1969-12-31"
,"prom_start_time":"2016-06-10 08:06:00","prom_end_time":"2016-06-10 11:06:00","client_order_id":"12365478996325412"
,"job_num":"4043269","campaign_wkord_obj_guid":"a9a8cc0b-d7ef-ac0e-df61-dad41c023cb0","pos_obj_guid"
:"096e38c2-55c3-80cf-0778-23f81f1cf2f6","inst_obj_guid":"fb91cf85-381c-b740-e063-775151743da2","client_obj_guid"
:"247e893a-3ea4-c544-47b2-f23ff16017c6","ord_obj_guid":"ae55e034-d7d0-5d13-3dd3-22f99df8ead4","skus"
:{"0":{"job_type_obj_guid":"8234ca2c-e40e-c48c-befc-7ceac5e9de32","job_sku":"3001","client_sku":null
,"sku_name":"Site Survey"},"1":{"job_type_obj_guid":"a3f60c3b-ad3c-d828-9898-fa200edcd3cd","job_sku"
:"4405","client_sku":null,"sku_name":"Home Delivery"}},"client_specific_data":""}}}
It appears as though new line feeds are being inserted randomly throughout the josn string. I am not sure if it is just how firebug is outputting the string, but copying-and-pasting the string into jsonLINT produces errors.
Any help would be appreciated.

How to get current prdocut category ID in Virtuemart [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Does anyone know what is conditional statement to detect a category id?
I need something like this:
If product is in category ID = 64 show some HTML
print_r($this->category);
TableCategories Object ( [virtuemart_category_id] => 16 [virtuemart_vendor_id] => 1 [category_name] => Chiloți, boxeri, indispensabili [slug] => chiloti-boxeri-indispensabili [category_description] => [category_template] => default [category_layout] => 0 [category_product_layout] => 0 [products_per_row] => 0 [ordering] => 3 [shared] => 0 [limit_list_step] => 0 [limit_list_initial] => 0 [metadesc] => [customtitle] => [metakey] => [metarobot] => [metaauthor] => [published] => 1 [_pkey:protected] => virtuemart_category_id [_pkeyForm:protected] => [_obkeys:protected] => Array ( [category_name] => Category in record is missing ! Can't save the record with no Category. [slug] => The given Sef Alias already exists. ) [_unique:protected] => 1 [_unique_name:protected] => Array ( [slug] => The given Sef Alias already exists. ) [_orderingKey:protected] => ordering [_slugAutoName:protected] => category_name [_slugName:protected] => slug [_loggable:protected] => 1 [_xParams:protected] => 0 [_varsToPushParam:protected] => Array ( ) [_translatable] => 1 [_translatableFields:protected] => Array ( [0] => category_name [1] => category_description [2] => metadesc [3] => metakey [4] => customtitle [slug] => slug ) [_langTag:protected] => en_gb [_tbl_lang:protected] => #__virtuemart_categories_en_gb [_updateNulls:protected] => [_tablePreFix] => c. [_tbl:protected] => #__virtuemart_categories [_tbl_key:protected] => virtuemart_category_id [_db:protected] => JFDatabase Object ( [mlTableList] => Array ( [0] => content [1] => modules [2] => menu ) [skipSetRefTables] => [orig_limit] => 0 [orig_offset] => 0 [skipjf] => 0 [translate] => 1 [tableFields:JFDatabase:private] => [profileData] => Array ( [JFDatabase::JFDatabase] => Array ( [total] => 0.0002288818359375 [count] => 1 [start] => Array ( ) ) [interceptDB::loadObjectList] => Array ( [total] => 0.0227241516113281 [count] => 62 [start] => Array ( [0] => 1397209811.22307 [1] => 1397209811.22406 [2] => 1397209811.22585 [3] => 1397209811.22628 [4] => 1397209811.23031 [5] => 1397209811.23254 [6] => 1397209811.24075 [7] => 1397209811.24117 [8] => 1397209811.24466 [9] => 1397209811.24608 [10] => 1397209811.27806 [11] => 1397209811.28038 [12] => 1397209811.28142 [13] => 1397209811.28238 [14] => 1397209811.28439 [15] => 1397209811.28616 [16] => 1397209811.28671 [17] => 1397209811.28773 [18] => 1397209811.28882 [19] => 1397209811.28975 [20] => 1397209811.29121 [21] => 1397209811.29171 [22] => 1397209811.29234 [23] => 1397209811.29328 [24] => 1397209811.29369 [25] => 1397209811.29412 [26] => 1397209811.2953 [27] => 1397209811.29915 [28] => 1397209811.29965 [29] => 1397209811.30009 [30] => 1397209811.30052 [31] => 1397209811.30295 [32] => 1397209811.304 [33] => 1397209811.30527 [34] => 1397209811.3057 [35] => 1397209811.30631 [36] => 1397209811.3067 [37] => 1397209811.30721 [38] => 1397209811.3123 [39] => 1397209811.32571 [40] => 1397209811.32638 [41] => 1397209811.32792 [42] => 1397209811.32891 [43] => 1397209811.32985 [44] => 1397209811.3308 [45] => 1397209811.33191 [46] => 1397209811.33233 [47] => 1397209811.333 [48] => 1397209811.33367 [49] => 1397209811.33744 [50] => 1397209811.33793 [51] => 1397209811.3384 [52] => 1397209811.33884 [53] => 1397209811.33928 [54] => 1397209811.34905 [55] => 1397209811.3519 [56] => 1397209811.35311 [57] => 1397209811.35398 [58] => 1397209811.35685 [59] => 1397209811.35732 ) ) [JFDatabase::getTableName] => Array ( [total] => 0.00406312942504883 [count] => 59 [start] => Array ( ) ) [JFDatabase::setLanguage] => Array ( [total] => 0.000144720077514648 [count] => 2 [start] => Array ( ) ) [JFDatabase::loadResult] => Array ( [total] => 0.000228643417358398 [count] => 13 [start] => Array ( ) ) [JFDatabase::loadAssoc] => Array ( [total] => 0.000162124633789062 [count] => 7 [start] => Array ( ) ) [JFDatabase::loadAssocList] => Array ( [total] => 0.000132083892822266 [count] => 4 [start] => Array ( ) ) [JFDatabase::loadResultArray] => Array ( [total] => 0.000250816345214844 [count] => 14 [start] => Array ( ) ) [JFDatabase::loadRow] => Array ( [total] => 0 [count] => 2 [start] => Array ( [0] => 1397209811.30335 [1] => 1397209811.30605 ) ) ) [name] => mysqli [nameQuote:protected] => ` [nullDate:protected] => 0000-00-00 00:00:00 [dbMinimum:protected] => 5.0.4 [_database:JDatabase:private] => 7624-magazin [connection:protected] => mysqli Object ( [affected_rows] => 0 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 1 [host_info] => ns13.host-md.net via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.5.32-MariaDB-log [server_version] => 50532 [stat] => Uptime: 346321 Threads: 6 Questions: 164660555 Slow queries: 35 Opens: 1823800 Flush tables: 293 Open tables: 1024 Queries per second avg: 475.456 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 2168966 [warning_count] => 0 ) [count:protected] => 0 [cursor:protected] => mysqli_result Object ( [current_field] => [field_count] => [lengths] => [num_rows] => [type] => ) [debug:protected] => [limit:protected] => 0 [log:protected] => Array ( ) [offset:protected] => 0 [sql:protected] => SELECT `virtuemart_vendor_id` FROM `#__virtuemart_vmusers` `au` WHERE `au`.`virtuemart_user_id`="687" AND `au`.`user_is_vendor` = "1" [tablePrefix:protected] => l50ti_ [utf:protected] => 1 [errorNum:protected] => 0 [errorMsg:protected] => [hasQuoted:protected] => [quoted:protected] => Array ( ) ) [_trackAssets:protected] => [_rules:protected] => [_locked:protected] => [_errors:protected] => Array ( ) [created_on] => 2014-03-16 21:10:24 [created_by] => 685 [modified_on] => 2014-04-08 13:29:22 [modified_by] => 685 [virtuemart_media_id] => Array ( ) [haschildren] => [children] => Array ( ) [productcount] => 6 [parents] => [images] => Array ( [0] => VmMediaHandler Object ( [media_attributes] => 0 [setRole] => [file_name] => [file_extension] => [virtuemart_media_id] => 0 [_foldersToTest:VmMediaHandler:private] => Array ( [0] => /var/www/vhosts/7624/domains/maicom.md/public_html/images/stories/virtuemart/category/ [1] => /var/www/vhosts/7624/domains/maicom.md/public_html/images/stories/virtuemart/category/resized/ ) [_actions:VmMediaHandler:private] => Array ( ) [_mLocation:VmMediaHandler:private] => Array ( ) [_hidden:VmMediaHandler:private] => Array ( ) [theme_url] => http://maicom.md/components/com_virtuemart/ [virtuemart_vendor_id] => 0 [file_title] => [file_description] => [file_meta] => [file_mimetype] => [file_type] => categories [file_url] => images/stories/virtuemart/category/ [file_url_thumb] => [published] => 0 [file_is_downloadable] => 0 [file_is_forSale] => 0 [file_is_product_image] => 0 [shared] => 0 [file_params] => 0 [file_lang] => [_translatable] => [_tablePreFix] => [created_on] => [created_by] => 0 [modified_on] => [modified_by] => 0 [file_url_folder] => images/stories/virtuemart/category/ [file_path_folder] => images/stories/virtuemart/category/ [file_url_folder_thumb] => images/stories/virtuemart/category/resized/ [media_role] => file_is_displayable ) ) [file_url_thumb] => [file_url] => images/stories/virtuemart/category/ ) </div>
What are you trying so far ?
did you need it in details page ?
if yes then try following method. I assume you are in any of the product details page layout.
if($this->product->virtuemart_category_id == 64){
echo 'your Custom HTML';
}
else{
//nothing
}
Also you need to get more details about current product just use.
echo "<pre/>";
print_r($this->product);
Hope its works..

Changing structure of array in php

I am completely new to programming and was tasked with changing the structure of an array, I just can't get it to work.
This is the original array:
[0] => Array
(
[att_values_id] => 5
[att_value] => Sloping
[att_id] => 5
[att_category] => Frame
)
[1] => Array
(
[att_values_id] => 13
[att_value] => Time Trial
[att_id] => 5
[att_category] => Frame
)
[2] => Array
(
[att_values_id] => 21
[att_value] => Mountain
[att_id] => 5
[att_category] => Frame
)
[3] => Array
(
[att_values_id] => 15
[att_value] => Carbon
[att_id] => 3
[att_category] => Material
)
[4] => Array
(
[att_values_id] => 15
[att_value] => Titanium
[att_id] => 9
[att_category] => Frame
)
[5] => Array
(
[att_values_id] => 15
[att_value] => Aluminum
[att_id] => 17
[att_category] => Frame
)
[6] => Array
(
[att_values_id] => 7
[att_value] => Expensive
[att_id] => 12
[att_category] => Price
)
[7] => Array
(
[att_values_id] => 7
[att_value] => Moderate
[att_id] => 33
[att_category] => Price
)
[8] => Array
(
[att_values_id] => 7
[att_value] => Entry Level
[att_id] => 40
[att_category] => Price
)
And I have to change it to this:
Array
(
[Frame] => Array
(
[5] => Sloping
[13] => Mountain
[21] => Time Trial
)
[Material] => Array
(
[3] => Carbon
[9] => Titanium
[17] => Aluminum
)
[Price] => Array
(
[12] => Expensive
[33] => Moderate
[40] => Entry Level
)
)
I have tried using foreach to go through the array but I don't know how to build the new array.
this little pseudocode is the closest we can get before plainly doing your homework.
foreach (firstarray as key => value) {
newarray[value['att_category']][value['att_id']]=value['att_value'];
}

How to sort array based on the timestamp in php

I have a array:
Array
(
[ID] => Array
(
[0] => 45335
[1] => 44403
[2] => 45734
[3] => 44494
[4] => 46869
[5] => 46895
[6] => 47481
[7] => 48788
[8] => 43950
[9] => 43960
[10] => 43979
)
[post_date] => Array
(
[0] => 1373812230
[1] => 1373835652
[2] => 1373900427
[3] => 1373922044
[4] => 1374087613
[5] => 1374094854
[6] => 1374354008
[7] => 1374613236
[8] => 1373547614
[9] => 1373558434
[10] => 1373569213
)
)
How to sort value post_date on DESC ?
my code here:
$indexarray = array();
array_multisort($indexarray, $indexarray["post_date"], SORT_DESC);
<?php
$original = array (
'ID' => array (
0 => 45335,
1 => 44403,
2 => 45734,
3 => 44494,
4 => 46869,
5 => 46895,
6 => 47481,
7 => 48788,
8 => 43950,
9 => 43960,
10 => 43979,
),
'post_date' => array
(
0 => 1373812230,
1 => 1373835652,
2 => 1373900427,
3 => 1373922044,
4 => 1374087613,
5 => 1374094854,
6 => 1374354008,
7 => 1374613236,
8 => 1373547614,
9 => 1373558434,
10 => 1373569213,
)
);
array_multisort($original['post_date'], SORT_DESC, $original['ID']);
var_dump($original);
This keeps your relation between $original['ID'][$i] and $original['post_date'][$i]. Result:
array(2) {
'ID' =>
array(11) {
[0] =>
int(48788)
[1] =>
int(47481)
[2] =>
int(46895)
[3] =>
int(46869)
[4] =>
int(44494)
[5] =>
int(45734)
[6] =>
int(44403)
[7] =>
int(45335)
[8] =>
int(43979)
[9] =>
int(43960)
[10] =>
int(43950)
}
'post_date' =>
array(11) {
[0] =>
int(1374613236)
[1] =>
int(1374354008)
[2] =>
int(1374094854)
[3] =>
int(1374087613)
[4] =>
int(1373922044)
[5] =>
int(1373900427)
[6] =>
int(1373835652)
[7] =>
int(1373812230)
[8] =>
int(1373569213)
[9] =>
int(1373558434)
[10] =>
int(1373547614)
}
}
Should be like:
array_multisort($indexarray["post_date"], SORT_DESC, SORT_STRING);
this should work.
array_multisort($arr["post_date"], SORT_DESC,$arr["ID"],SORT_DESC);
refer http://php.net/manual/en/function.array-multisort.php
Try this:
<?php
$array = array (
'ID' => array (
0 => 45335,
1 => 44403,
2 => 45734,
3 => 44494,
4 => 46869,
5 => 46895,
6 => 47481,
7 => 48788,
8 => 43950,
9 => 43960,
10 => 43979,
),
'post_date' => array
(
0 => 1373812230,
1 => 1373835652,
2 => 1373900427,
3 => 1373922044,
4 => 1374087613,
5 => 1374094854,
6 => 1374354008,
7 => 1374613236,
8 => 1373547614,
9 => 1373558434,
10 => 1373569213,
)
);
array_multisort($array['post_date'], SORT_DESC, $array['post_date']);
print_r($array);

Categories