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)
As you see each attribute in my JSON array carries array. SO how do I combine the first value of all arrays into one single array and the second value of all into another array? Also how to extract each value like $data['?'] after that.
I want to insert into database all the items for one ID and then followed by next ID's items. How to arrange the array in such a way please?
DESIRED OUTPUT:
$array1 = array("ID"=>"33","group" => "ef4", "item" => "apple", "harga" => "56".....);
$array2 = array("ID"=>"33","group" => "rfrf4", "item" => "a45erf4e", "harga" => "54".....);
This is an example with multisort.
<?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"] ;
}
?>
PHP function 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")
*/
Try the following:
<?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);
The arrays should then be stored in the $arrays
variable.
Example output:
$arrays = array (
0 => array (
'contract_id' => '33',
'group' => '1',
'item' => '1',
'harga_1' => '4',
'qty' => '2',
'amount' => '8',
),
1 => array (
'contract_id' => '33',
'group' => '2',
'item' => 'No items found',
'harga_1' => '1',
'qty' => '2',
'amount' => '2',
),
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.