如何在PHP中调用和打印“多维数组函数”?

时间:2019-04-20 15:01:38

标签: php

我正在创建一个称为多维数组的函数,我想将其打印到浏览器中。我需要在哪里展示?

这是针对新的后端Web脚本的,该脚本在Apache2,MariaDB 10,PHP 7中运行。这一次,我尝试添加isset函数,但效果不佳。

<?php

//function Multidimentional Array
function barang (){

$charles = [["nama_barang"=>"Laptop","harga"=>15000,"Qty"=>100,"Kondisi"=>"Baik"],["nama_barang"=>"Dekstop","harga"=>30000,"Qty"=>30,"Kondisi"=>"Buruk"],
["nama_barang"=>"Smartphone","harga"=>40000,"Qty"=>20,"Kondisi"=>"Baik Sekali"]];


}

//Print the result of Multidimentional Array called from function
$oke = barang();

echo $oke;

?>

我希望输出为“笔记本电脑”,15000、100,Baik,但实际输出为无。

2 个答案:

答案 0 :(得分:0)

我无法完全理解您的问题。但是据我了解,您需要像这样的"Laptop",15000, 100, Baik

输出
<?php

//function Multidimentional Array
function barang (){

$charles = [["nama_barang"=>"Laptop","harga"=>15000,"Qty"=>100,"Kondisi"=>"Baik"],["nama_barang"=>"Dekstop","harga"=>30000,"Qty"=>30,"Kondisi"=>"Buruk"],
["nama_barang"=>"Smartphone","harga"=>40000,"Qty"=>20,"Kondisi"=>"Baik Sekali"]];

return $charles;
}

//Print the result of Multidimentional Array called from function
echo '"' . barang()[0]['nama_barang'] . '" ,' .
 barang()[0]['harga'] . ' ,' .
 barang()[0]['Qty'] . ' , ' .
 barang()[0]['Kondisi'];

?>

这里是demo

答案 1 :(得分:0)

您可以使用array_map在浏览器上打印它们

$charles = 
[["nama_barang"=>"Laptop","harga"=>15000,"Qty"=>100,"Kondisi"=>"Baik"], 
["nama_barang"=>"Dekstop","harga"=>30000,"Qty"=>30,"Kondisi"=>"Buruk"],
["nama_barang"=>"Smartphone","harga"=>40000,"Qty"=>20,"Kondisi"=>"Baik Sekali"]];

$res = array_map(function($v){
    return implode(',',$v); //echo implode(',',$v)
}, $charles);

echo '<pre>';
print_r($res);