多维数组,如果存在数组值,则在该值之后创建新数组

时间:2013-02-22 04:24:30

标签: php arrays multidimensional-array

我决定设置一个数组来区分某些值。如果我想在数组中找到某个值,我想在我搜索过的这个值中使用其余的数据:

Array
(
    [0] => Array
        (
            [id] => 123
            [0] => Array
                (
                    [name] => Burger Joint
                    [pluralName] => Burger Joints
                    [shortName] => Burgers
                )

        )

    [1] => Array
        (
            [id] => 617
            [0] => Array
                (
                    [name] => reeeJoint
                    [pluralName] => reeeJoints
                    [shortName] => reee
                )

        )

    [2] => Array
        (
            [id] => 12355
            [0] => Array
                (
                    [name] => LeftJoint
                    [pluralName] => LeftJoints
                    [shortName] => Left
                )

        )
    [4] => Array
        (
            [id] => 526
            [0] => Array
                (
                    [name] => asdfJoint
                    [pluralName] => asdfJoints
                    [shortName] => asdf
                )

        )
)

我想进行某种搜索,如果有123的值,它只会获得一个包含123 id数组中所有内容的新数组(在这种情况下是第一个)。 / p>

找到这个的最佳做法是什么?

谢谢!

编辑:

根据一些评论,我能够想出一种新的设置阵列的方法。然而,我仍然想知道同样的问题,以解决价值存在的方法:

foreach($values as $value)
{
 ...
   $categoriesExtract[$id] = array($category_stringArray2Sugg);
}

给了我这个数组:

Array
(
    [123] => Array
        (
            [0] => Array
                (
                    [name] => Burger Joint
                    [pluralName] => Burger Joints
                    [shortName] => Burgers
                )

        )

    [617] => Array
        (
            [0] => Array
                (
                    [name] => reeeJoint
                    [pluralName] => reeeJoints
                    [shortName] => reee
                )

        )

    [12355] => Array
        (
            [0] => Array
                (
                    [name] => LeftJoint
                    [pluralName] => LeftJoints
                    [shortName] => Left
                )

        )
    [526] => Array
        (
            [0] => Array
                (
                    [name] => asdfJoint
                    [pluralName] => asdfJoints
                    [shortName] => asdf
                )

        )
)

5 个答案:

答案 0 :(得分:1)

使用此代码。它将在比赛后创建新阵列。

 $arr = array();
 for($i = 0;$i<count($array);$i++)
 {
     if($array[$i][id] == 123)
     {
          $arr[] = $array[$i][0];
     }
 }
 print_r($arr);

答案 1 :(得分:1)

为什么不使用id作为密钥,然后拔出密钥?

$array = Array
(
    [123] => Array
        (
            [name] => Burger Joint
            [pluralName] => Burger Joints
            [shortName] => Burgers
        )

    [617] => Array
        (
            [name] => reeeJoint
            [pluralName] => reeeJoints
            [shortName] => reee
        )
)


$new_array = $array[123];

您有2个ID为123的项目,这意味着这种情况不起作用,但在您的问题中,您表示您希望返回第一个值。 ID上的加倍是错误,还是可以合法发生的事情,因为这将意味着这不是一种合适的方式。

答案 2 :(得分:0)

你可以这么简单地做:

$array = array(0 => 
                  array('id' => 123,
                        0 => array('name' => 'Burger Joint',
                                   'pluralName' => 'Burger Joints',
                                   'shortName' => 'Burgers')
                                   )
                );

$new_array =  array();
foreach($array as $value){
   $new_array[$value['id']][] = $value[0];
}

输出

Array
(
    [123] => Array
        (
            [0] => Array
                (
                    [name] => Burger Joint
                    [pluralName] => Burger Joints
                    [shortName] => Burgers
                )

        )

)

Cheeers !!!!!

在此处查看工作演示working demo link

答案 3 :(得分:0)

尝试此解决方案:此处更改$find数组中的值以搜索ID

$array  = Array(Array("id" => 123,0 => Array("name" => "Burger Joint","pluralName" => "Burger Joints","shortName" => "Burgers")),
                Array("id" => 617,0 => Array("name" => "reeeJoint","pluralName" => "reeeJoints","shortName" => "reee")),
                Array("id" => 12355,0 => Array("name" => "LeftJoint","pluralName" => "LeftJoints","shortName" => "Left")),
                Array("id" => 526,0 => Array("name" => "asdfJoint","pluralName" => "asdfJoints","shortName" => "asdf"))
                );
$find   = array(123,526); /// Get all the array with this id

$result = array();
foreach($array as $vals){
    if(in_array($vals['id'],$find)){
        $result[]  = $vals;
    }
}

echo "<pre>";
print_r($result);

答案 4 :(得分:0)

我认为我只是让事情变得更加复杂......

由于这些建议,我只能通过这样做来获得数组

$阵列(123); =&GT;这只会让我得到值为123的数组......