Show data that is not contiguous

Source: Internet
Author: User
Show nonadjacent data
This post was last edited by lazygc520 on 2014-08-05 14:02:30

Known $arrs,

Array (

Array (
' Pono ' = ' a ',
' No ' = ' 4711 ',
),

Array (
' Pono ' = ' a ',
' No ' = ' 4714 ',
),

Array (
' Pono ' = ' B ',
' No ' = ' 4714 ',
),

Array (
' Pono ' = ' B ',
' No ' = ' 4715 ',
),

Array (
' Pono ' = ' C ',
' No ' = ' 4715 ',
),

Array (
' Pono ' = ' C ',
' No ' = ' 4716 ',
),
)


The data are now merged with Pono to:

$tmp =array ();
$data =array ();
$i = 0;
foreach ($arrs as $key = = $arr) {
if (!empty ($tmp) && array_key_exists ($arr [' Pono '], $tmp)) {
$key = $tmp [$arr [' Pono '];
$arr _no=explode (', ', $data [$key] [' no ']);
if (!in_array ($arr [' No '], $arr _no)) $data [$key] [' No '].= ', '. $arr [' no '];
}else{
$tmp [$arr [' Pono ']]= $i;
$data [$i ++]= $arr;
}
}
Print_r ($data);

Get results:
Array
(
[0] = = Array
(
[Pono] = a
[No] = 4711,4714
)

[1] = = Array
(
[Pono] = b
[No] = 4714,4715
)

[2] = = Array
(
[Pono] = C
[No] = 4715,4716
)
)


Now you want to show only nonadjacent no, such as the item in the example:
[0] = = Array
(
[Pono] = a
[No] = 4711,4714
)

How can I ask?
------Solution--------------------

$arrs = Array (
0 =
Array (
' Pono ' = ' a ',
' No ' = ' 4711 ',
),
1 =
Array (
' Pono ' = ' a ',
' No ' = ' 4714 ',
),
2 =
Array (
' Pono ' = ' B ',
' No ' = ' 4714 ',
),
3 =
Array (
' Pono ' = ' B ',
' No ' = ' 4715 ',
),
4 =
Array (
' Pono ' = ' C ',
' No ' = ' 4715 ',
),
5 =
Array (
' Pono ' = ' C ',
' No ' = ' 4716 ',
),
);


$tmp =array ();
$data =array ();
$i = 0;
foreach ($arrs as $key = = $arr) {
if (!empty ($tmp) && array_key_exists ($arr [' Pono '], $tmp)) {
$key = $tmp [$arr [' Pono '];
$arr _no=explode (', ', $data [$key] [' no ']);
if (!in_array ($arr [' No '], $arr _no)) $data [$key] [' No '].= ', '. $arr [' no '];
}else{
$tmp [$arr [' Pono ']]= $i;
$data [$i ++]= $arr;
}
}
Print_r ($data);

$result = Array ();

foreach ($data as $val) {
List ($a, $b) = Explode (', ', $val [' no ']);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.