dongyuan9892
2018-10-05 04:47
浏览 22
已采纳

获取父索引数组多维

I'm dealing with php array and I have proplem with get the index of parent array. Here I want to find parent index of title so I try find it by loop but I failed find index if the title is array. How I can get the parent index if the title is array

<?php
$Arr = [
    [
        "title" => ["news", "nasional", "internasional", "regional", "metropolitan", "sains", "pendidikan"],
        "url" => "AAA",
    ],
    [
        "title" => ["seleb", "news"],
        "url" => "BBB"
    ],
    [
        "title" => "travel",
        "url" => "CCC"
    ]
];


foreach($Arr as $value) {
$key = array_search("seleb", array_column($Arr , 'title'));
    echo $key; // should return index parent is [1]
}

?>

图片转代码服务由CSDN问答提供 功能建议

我正在处理php数组,我有了获取父数组索引的问题。 在这里,我想找到标题的父索引,所以我尝试通过循环找到它,但如果标题是数组我找不到索引。 如果标题是数组我怎么能得到父索引

 &lt;?php 
 $ Arr = [
 [
“title”=&gt;  [“news”,“nasional”,“internasional”,“regional”,“metropolitan”,“sains”,“pendidikan”],
“url”=&gt;  “AAA”,
],
 [
“title”=&gt;  [“seleb”,“news”],
“url”=&gt;  “BBB”
],
 [
“title”=&gt;  “travel”,
“url”=&gt;  “CCC”
] 
]; 
 
 
foreach($ Arr as $ value){
 $ key = array_search(“seleb”,array_column($ Arr,'title')); 
 echo $ 键;  //应该返回索引parent是[1] 
} 
 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douqiao1887 2018-10-05 05:23
    已采纳

    Try this to find the parent index

    $Arr = [
            [
              "title" => ["news", "nasional", "internasional", "regional", "metropolitan", "sains", "pendidikan"],
              "url" => "AAA",
           ],
           [
             "title" => ["seleb", "news"],
             "url" => "BBB"
           ],
           [
             "title" => "travel",
             "url" => "CCC"
           ]
          ];
    
       foreach($Arr as $key => $value) {
         if(is_array($value['title'])){
            if(in_array("seleb", $value['title']))
               echo $key;
         }
       }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dqhnp44220 2018-10-05 04:55

    You are passing the whole array instead of it pass the current value of the loop

    foreach ($Arr as $key => $value) {
        $keyChild='';
        $keyChild = array_search("seleb", $value);
        if ($keyChild != null && empty($keyChild)) {
            echo $key;
        }
    }
    
    评论
    解决 无用
    打赏 举报
  • duanbozhong9689 2018-10-05 10:07

    Here's another way of doing it with a little less nesting and a bit more re-usability

    <?php
    $arr = [
        [
            "title" => ["news", "nasional", "internasional", "regional", "metropolitan", "sains", "pendidikan"],
            "url" => "AAA",
        ],
        [
            "title" => ["seleb", "news"],
            "url" => "BBB"
        ],
        [
            "title" => "travel",
            "url" => "CCC"
        ]
    ];
    
    function findIndex($search, $array) {
        $found  = array_filter($array, function ($value) use ($search) {
            if (!is_array($value)) $value = [$value];
            return array_search($search, $value) !== false;
        });
    
        return key($found);
    }
    
    $titles = array_column($arr, 'title');
    echo findIndex('seleb', $titles); // 1
    

    findIndex will return the parent index or null deepening on whether the result is found or not.

    Also this part:

    $titles = array_column($arr, 'title');
    

    Could be placed inside the function, with the 'title' being passed as another parameter.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题