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



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

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

为什么不使用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];

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

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

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

您可以简单地这样做:

$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
                )
        )
)

奶酪!!!!!

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

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

$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);

我想我只是把一些简单的事情变得更复杂了。。。

由于这些建议,我只能通过做这个来获得阵列

$array(123);=>这只会得到值为123的数组…

相关内容

  • 没有找到相关文章

最新更新