PHP Array Filtering issues

Source: Internet
Author: User
Hypothetical array
Array (0 + = array (' catid ' = ' = ' Parent ' = ' 1 '), 1 = = Array (' catid ' + = = ' Parent ' = 1), Array (' C Atid ' = +, ' parent ' = 2);

I want to keep only the value of parent 2 What do I do?
New brother hope the hero help thank you!


Reply to discussion (solution)

$a = array (' catid ' = ' = ', ' parent ' = ' 1 '), 1 = = Array (' catid ' + = = ' Parent ' = 1), arr Ay (' catid ', ' parent ' = 2)); $t = Array_map (function ($v) {return Array (' parent ' = = $v [' parent ']);}, $a); /*php 5.2.x Writing $t = Array_map (create_function (' $v ', ' Return array ' ("parent" = + $v ["parent"]); '), $a); */print_r ($t);
Array
(
[0] = = Array
(
[Parent] = 1
)

[1] = = Array
(
[Parent] = 1
)

[2] = = Array
(
[Parent] = 2
)

)

function intersect2nd ($arr 1, $arr 2, $key)
{
foreach ($arr 2 as $v) $TMPARR [] = $v [$key];
foreach ($arr 1 as $k + $v) if (In_array ($v [$key], $TMPARR)) $arr [$k] = $v;
return $arr;
}
$a =array (0 = = Array (' catid ' = ' = ', ' parent ' = ' 1 '), 1 = = Array (' catid ' = = '), ' parent ' = 1), array (' catid ', ' parent ' = 2));
$b =array (Array (' parent ' = 2)); This is a similar array, containing the required key and value.
$c = intersect2nd ($a, $b, ' parent ');
Var_export ($c);

Memory of this is the moderator nagging code, dare not to usurp

PHP code?1234567$a = Array (0 = = Array (' catid ' = ' = ', ' parent ' = ' 1 '), 1 = = Array (' catid ' + = ' Parent ' = = 1 ', array (' catid ' = +, ' parent ' = 2)); $t = Array_map (function ($v) {R ...
First Thank you Moderator

But that's not true, I might not be able to express it very clearly, I was hoping to keep the parent's value of 2 in the array, that is, to keep the array (' catid ' = +, ' parent ' = 2).

function intersect2nd ($arr 1, $arr 2, $key)
{
foreach ($arr 2 as $v) $TMPARR [] = $v [$key];
foreach ($arr 1 as $k + $v) if (In_array ($v [$key], $TMPARR)) $arr [$k] = $v;
return $arr;
}
$a =arra ...
Thank you for having solved it.

Obviously it's not my code, because it doesn't fit my writing style.

function intersect2nd ($arr 1, $arr 2, $key)
{
foreach ($arr 2 as $v) $TMPARR [] = $v [$key];
foreach ($arr 1 as $k + $v) if (In_array ($v [$key], $TMPARR)) $arr [$k] = $v;
return $arr;
}
$a =arra ...

If you don't like the callback function, you can write this.

$a = array (' catid ' = ' = ', ' parent ' = ' 1 '), 1 = = Array (' catid ' + = = ' Parent ' = 1), arr Ay (' catid ' = ' parent ' = 2)), foreach ($a as $r) {  $t [] = Array_intersect_key ($r, Array (' parent ' = ') ') ;}

Obviously it's not my code, because it doesn't fit my writing style.

Reference 2 Floor snmr_com's reply: function intersect2nd ($arr 1, $arr 2, $key)
{
foreach ($arr 2 as $v) $TMPARR [] = $v [$key];
foreach ($arr 1 as $k + $v) if (In_array ($v [$key], $tmpA ...

Hey, I said is the idea, I changed to own class inside a method used, now copy changed version came out

But that's not true, I might not be able to express it very clearly, I was hoping to keep the parent's value of 2 in the array, that is, to keep the array (' catid ' = +, ' parent ' = 2). Oh, write it like this.

$a = array (' catid ' = ' = ', ' parent ' = ' 1 '), 1 = = Array (' catid ' + = = ' Parent ' = 1), arr Ay (' catid ' = ' parent ' = 2)); $t = Array_filter ($a, function ($v) {return $v [' parent '] = = 2;}); Print_r ($t);
Array
(
[2] = = Array
(
[CATID] = 21
[Parent] = 2
)

)
  • 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.