如何将多个数组合并为一个?

时间:2015-10-09 08:20:14

标签: php arrays

Json string : $return ='{"contract_id":["33"],"group":["1","2"],"item":["1","No items found"],"harga_1":["4","1"],"qty":["2","2"],"amount":["8","2"],"action":"test"}';

$data = json_decode($return, true);
var_dump($data['item']);
array (size=2)
  0 => string '1' (length=1)
  1 => string 'No items found' (length=14)
var_dump($data['group']);
output:
array (size=2)


 0 => string '1' (length=1)
  1 => string '2' (length=1)

如您所见,我的JSON数组中的每个属性都包含数组。那么如何将所有数组的第一个值组合成一个单独的数组,将all的第二个值组合到另一个数组中呢?此外,如何在$ data ['?']之后提取每个值。

我想在数据库中插入一个ID的所有项目,然后是下一个ID项目。如何以这种方式安排阵列?

期望的输出:

$array1 = array("ID"=>"33","group" => "ef4", "item" => "apple", "harga" => "56".....);
$array2 = array("ID"=>"33","group" => "rfrf4", "item" => "a45erf4e", "harga" => "54".....);

3 个答案:

答案 0 :(得分:0)

这是多重排序的一个例子。

<?php 
        error_reporting(0);
         $multiArray = array( 
                    1,9,3,
                    array(90,20,70),        
                    'a',
                    'x',
                    'p',
                    array("name" => "Defg"), 
                    array("name" => "Abcd"),
                    array("name" => "Eefg"), 
                    array("name" => "Fbcd"),
                    array(9,2,8)
                   );



     $length=count($multiArray);

    array_multisort($multiArray[3], SORT_DESC,SORT_NUMERIC);
    array_multisort($multiArray[11], SORT_ASC,SORT_NUMERIC);

    sort($multiArray);
    var_dump($multiArray);
    $tmp = array(); 
    foreach($multiArray as &$ma) 
   {
     $tmp[] = &$ma["name"];
   } 
    echo "<br>";

         //array_multisort($tmp, SORT_DESC,$multiArray);  

             foreach($multiArray as &$ma)
      {
        "<br>" . $ma["name"] ;
       }     

     ?>

答案 1 :(得分:0)

PHP函数array_column

<?php

//JSON
$json  ='{"contract_id":["33"],"group":["A","B"],"item":["A1","BNo items found"],"harga_1":["A4","B1"],"qty":["A2","B2"],"amount":["A8","B2"],"action":"test"}';
$data = json_decode($json, true);

// Contract
$contractId = array_shift($data); // [33]   

$firstRow = array_column($data, 0);
$secondRow = array_column($data, 1);

/* result:
 *
 * firstRow => array("A","A1","A4","A2","A8")
 * secondRow => array("B","BNo items found","B1","B2","B2")
 */

答案 2 :(得分:0)

尝试以下方法:

$em = $this->getContainer()->get('doctrine')->getManager();
    $pages=$em->getRepository('PoznetAdminBundle:page')->findAll();
        $ile=count($pages);
        for($i=0;$i<$ile;$i++){
             $pages[$i]->setSlug($pages[$i]->getTitle());
        }
    $em->flush();

然后应将数组存储在<?php $data = json_decode('{"contract_id":["33"],"group":["1","2"],"item":["1","No items found"],"harga_1":["4","1"],"qty":["2","2"],"amount":["8","2"],"action":"test"}', true); $values = array( 'contract_id' , 'group' , 'item' , 'harga_1' , 'qty' , 'amount' ); $arrays = array(); for($i = 0; $i < count($data['item']); $i++){ $arrays[$i] = array(); foreach($values as $value){ $arrays[$i][$value] = isset($data[$value][$i]) ? $data[$value][$i]: $data[$value][0]; } } var_dump($arrays); 变量中。

示例输出:

$arrays