0

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

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 数组中的所有内容(在这种情况下是第一个)。

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

谢谢!

编辑:

根据一些评论,我能够想出一种设置数组的新方法。但是我仍然想知道同样的问题,如果价值存在,用哪种方法来解决:

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
                )

        )
)
4

5 回答 5

1

使用此代码。它将在匹配后创建新数组。

 $arr = array();
 for($i = 0;$i<count($array);$i++)
 {
     if($array[$i][id] == 123)
     {
          $arr[] = $array[$i][0];
     }
 }
 print_r($arr);
于 2013-02-22T04:29:02.940 回答
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 上的加倍是错误,还是可以合法发生的事情,因为这意味着这不是一种合适的方式。

于 2013-02-22T04:30:16.360 回答
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
                )

        )

)

干杯!!!!

在此处查看工作演示工作演示链接

于 2013-02-22T04:55:08.293 回答
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);
于 2013-02-22T04:57:15.193 回答
0

我想我只是让一些简单的事情变得更复杂......

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

$数组(123);=> 这只会让我得到值为 123 的数组...

于 2013-02-22T05:06:56.290 回答