Below given is my array structure
Array
(
[0] => Array
(
[product_id] => 59
[product_name] => Samsung Champ DUOS E2652
[product_price] => 4439
[sub_cat_name] => Samsung
[sub_cat_url] => samsung
[image] => samsung-champ-duos-e2652---1414801404364308.jpg
[currency] => ₹
)
[1] => Array
(
[product_id] => 195
[product_name] => Samsung Galaxy Tab 4 7.0
[product_price] => 17847
[sub_cat_name] => Samsung
[sub_cat_url] => samsung
[image] => galaxy-tab-4-7-0---1715601405057269.png
[currency] => ₹
)
[2] => Array
(
[product_id] => 284
[product_name] => Samsung Galaxy Tab 3 Lite 7.0
[product_price] => 18000
[sub_cat_name] => Samsung
[sub_cat_url] => samsung
[image] => galaxy-tab-3-lite-7-0---1590061405576878.png
[currency] => ₹
)
[3] => Array
(
[product_id] => 19
[product_name] => Samsung Galaxy Star
[product_price] => 4833
[sub_cat_name] => Samsung
[sub_cat_url] => samsung
[image] => feature-img144811404109906.jpg
[currency] => ₹
)
[4] => Array
(
[product_id] => 186
[product_name] => Samsung Galaxy S5 Active
[product_price] => 31922
[sub_cat_name] => Samsung
[sub_cat_url] => samsung
[image] => galaxy-s5-active368091404989896.png
[currency] => ₹
)
)
i would like to based on sub_cat_url
like below 基于sub_cat_url
进行
array
(
[0] => samsung
[0] => Array
(
[product_id] => 186
[product_name] => Samsung Galaxy S5 Active
[product_price] => 31922
[sub_cat_name] => Samsung
[sub_cat_url] => samsung
[image] => galaxy-s5-active368091404989896.png
[currency] => ₹
)
[1] => Array
(
[product_id] => 284
[product_name] => Samsung Galaxy Tab 3 Lite 7.0
[product_price] => 18000
[sub_cat_name] => Samsung
[sub_cat_url] => samsung
[image] => galaxy-tab-3-lite-7-0---1590061405576878.png
[currency] => ₹
)
)
for thet I wrote a code like below.
for($i = 0; $i < $count; $i++){
if($i == 0){
$search_array[$i] = $search_data[$i]["sub_cat_name"];
$search_array[$i] = $search_data[$i]["product_name"];
$j += 1;
}
else if($search_data[$i]["sub_cat_url"] !== $search_data[$i]["sub_cat_url"]){
$search_array[$i] = array($search_data[$i]["sub_cat_name"]);
$search_array[$i] = array($search_data[$i]["product_name"]);
$j += 1;
}
else if($search_data[$i]["sub_cat_url"] === $search_data[$i]["sub_cat_url"]){
$search_array[$i] = array($search_data[$i]["product_name"]);
$j += 1;
}
}
but it didn't work well. And I tried using inner for loop also. It too didn't give me the actual result. Can some one please hlp me to write the correct loop statement for this? Thank you in advance
If I understood you well this should work:
$out = array();
$map = array();
foreach ($array as $k => $v) {
$url = $v['sub_cat_url'];
if (!isset($map[$url])) {
$map[$url] = count($out);
}
$out[$map[$url]][$url][$k] = $v; // or $out[$map[$url]][$url][] = $v;
}
However I haven't tested it (you haven't provided data in PHP format)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.