如何搜索具有特定ID的子数组并从其元素生成新数组?

时间:2017-03-30 04:42:05

标签: php arrays search multidimensional-array foreach

我一直在尝试将数据组织成一个来自foreach循环的多维数组,但数据到处都是。

这就是我编码的内容:

$productIDs = array(
    '0' => array(
        'product_id' => '10',
        'product_name' => 'Test',
        'product_file' => 'file10',
        'product_image' => 'https://i.imgur.com/dXb6.png',
    ),
    '1' => array(
        'product_id' => '20',
        'product_name' => 'Test1',
        'product_file' => 'file20',
        'product_image' => 'https://i.imgur.com/MuP8.png',
    ),
    '2' => array(
        'product_id' => '30',
        'product_name' => 'No product',
        'product_file' => 'file30',
        'product_image' => 'https://i.imgur.com/kWP3.png',
    )
);

$urlIDs  = array(10,20);

function getFiles($productIDs, $urlIDs)
{   
    foreach($productIDs as $ids)
    {
        foreach($urlIDs as $products)
        {   
            if(in_array($products, $ids)){
                $data1[] = $ids['product_id'];
                $data2[] = $ids['product_file'];
                $data3[] = $ids['product_image'];
            }
        }
    }
    return array($data1, $data2, $data3);
}
$getFiles = getFiles($productIDs, $urlIDs);

以下输出为:

Array
(
    [0] => Array
        (
            [0] => 10
            [1] => 20
        )

    [1] => Array
        (
            [0] => file10
            [1] => file20
        )

    [2] => Array
        (
            [0] => https://i.imgur.com/dXb6.png
            [1] => https://i.imgur.com/MuP8.png
        )

)

虽然我想要完成的是:

Array
(
    [0] => Array
        (
            [id] => 10
            [file] => file10
            [image] => https://i.imgur.com/dXb6.png
        )

    [1] => Array
        (
            [id] => 20
            [file] => file20
            [image] => https://i.imgur.com/MuP8.png
        )

)

我尝试了以下内容:

function getFiles($productIDs, $urlIDs)
{   
    foreach($productIDs as $ids)
    {
        foreach($urlIDs as $products)
        {   
            if(in_array($products, $ids)){
                $data['id'] = $ids['product_id'];
                $data['file'] = $ids['product_file'];
                $data['image'] = $ids['product_image'];
            }
        }
    }
    return array($data);
}

返回以下内容而不循环遍历所有id,它应返回两个数组,因为两个id都匹配。

Array
(
    [0] => Array
        (
            [id] => 20
            [file] => file20
            [image] => https://i.imgur.com/MuP8.png
        )

)

你可以说没有特定键名的第一批代码不是我想要的,但我只想展示我曾经尝试过的东西。最后一批工作(带有键名),但没有循环遍历所有$urlIDs,由于某些原因我无法理解,代码甚至没有返回id = 10,它返回id = 20,虽然它是阵列中的第二个。如果有人能解释为什么会这样,我会很感激。

如果它有用:https://eval.in/764083

5 个答案:

答案 0 :(得分:1)

PHP code demo

<?php
$productIDs = array(
    '0' => array(
        'product_id' => '10',
        'product_name' => 'Test',
        'product_file' => 'file10',
        'product_image' => 'https://i.imgur.com/dXb6.png',
    ),
    '1' => array(
        'product_id' => '20',
        'product_name' => 'Test1',
        'product_file' => 'file20',
        'product_image' => 'https://i.imgur.com/MuP8.png',
    ),
    '2' => array(
        'product_id' => '30',
        'product_name' => 'No product',
        'product_file' => 'file30',
        'product_image' => 'https://i.imgur.com/kWP3.png',
    )
);

$urlIDs  = array(10,20);

function getFiles($productIDs, $urlIDs)
{   
    $result=array();
    foreach($productIDs as $key => $productData)
    {
        if(!in_array($productData['product_id'],$urlIDs))
        {
            unset($productIDs[$key]);
        }
        else
        {
            $result[]=array("id"=>$productData['product_id'],"file"=>$productData['product_file'],"image"=>$productData['product_image']);
        }
    }

    return $result;
}
$getFiles = getFiles($productIDs, $urlIDs);
print_r($getFiles);

<强>输出:

Array
(
    [0] => Array
        (
            [id] => 10
            [file] => file10
            [image] => https://i.imgur.com/dXb6.png
        )

    [1] => Array
        (
            [id] => 20
            [file] => file20
            [image] => https://i.imgur.com/MuP8.png
        )

)

答案 1 :(得分:1)

试试这个。

function getFiles($productIDs, $urlIDs)
{   
$newDara = [];
    foreach($productIDs as $ids)
    {
        foreach($urlIDs as $products)
        {   
            if(in_array($products, $ids)){
                $data['id'] = $ids['product_id'];
                $data['file'] = $ids['product_file'];
                $data['image'] = $ids['product_image'];
                $newData[] = $data;
            }
        }
    }
    return array($newData);
}

答案 2 :(得分:1)

利用您尝试过的解决方案,将您尝试过的解决方案更改为此。

function getFiles($productIDs, $urlIDs)
{

    foreach($productIDs as $ids)
    {
        foreach($urlIDs as $products)
        {
            if(in_array($products, $ids)){
                $data[] = [ "id" => $ids['product_id'],
                            "file" => $ids['product_file'],
                            "image" => $ids['product_image']];
            }
        }
    }
    return $data;
}

答案 3 :(得分:1)

试用此版本

    <?php
    /**
     * Created by PhpStorm.
     * User: lenovo
     * Date: 3/30/2017
     * Time: 5:53 AM
     */
    $productIDs = array(
        '0' => array(
            'product_id' => '10',
            'product_name' => 'Test',
            'product_file' => 'file10',
            'product_image' => 'https://i.imgur.com/dXb6.png',
        ),
        '1' => array(
            'product_id' => '20',
            'product_name' => 'Test1',
            'product_file' => 'file20',
            'product_image' => 'https://i.imgur.com/MuP8.png',
        ),
        '2' => array(
            'product_id' => '30',
            'product_name' => 'No product',
            'product_file' => 'file30',
            'product_image' => 'https://i.imgur.com/kWP3.png',
        )
    );

    $urlIDs  = array(10,20);

    $ouput = [];

    foreach($productIDs as $product){

        if(in_array($product['product_id'],$urlIDs)){
           $ouput[] = array("id"=>$product["product_id"], "file"=>$product["product_file"], "image"=>$product["product_image"]);
        }
    }

   echo "<pre>";
     print_r($ouput);
    echo "</pre>";

检查output here

祝你好运

答案 4 :(得分:0)

其他答案要么是做了太多的循环,要么循环遍历$productIDs数组(我假设你的实际项目要长得多)。

此方法只会迭代您的搜索数组,这应该会提升性能。您会看到此方法发布为@ http://php.net/manual/en/function.array-search.php

的最高评论

代码:(Demo

$productIDs = array(
    '0' => array(
        'product_id' => '10',
        'product_name' => 'Test',
        'product_file' => 'file10',
        'product_image' => 'https://i.imgur.com/dXb6.png',
    ),
    '1' => array(
        'product_id' => '20',
        'product_name' => 'Test1',
        'product_file' => 'file20',
        'product_image' => 'https://i.imgur.com/MuP8.png',
    ),
    '2' => array(
        'product_id' => '30',
        'product_name' => 'No product',
        'product_file' => 'file30',
        'product_image' => 'https://i.imgur.com/kWP3.png',
    )
);
$urlIDs=array(10,20);
foreach($urlIDs as $prd_id){
    if(($i=array_search($prd_id,array_column($productIDs,'product_id')))!==false){
        $result[]=['id'=>$productIDs[$i]['product_id'],
                   'file'=>$productIDs[$i]['product_file'],
                   'image'=>$productIDs[$i]['product_image']
                  ];
    }else{
        echo "$prd_id not found";
    }
}
var_export($result);

输出:

array (
  0 => 
  array (
    'id' => '10',
    'file' => 'file10',
    'image' => 'https://i.imgur.com/dXb6.png',
  ),
  1 => 
  array (
    'id' => '20',
    'file' => 'file20',
    'image' => 'https://i.imgur.com/MuP8.png',
  ),
)