猿问

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

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


这是针对在Apache2,MariaDB 10,PHP 7中运行的新的后端Web脚本编写的。这次,我尝试添加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;


?>

我希望输出是“ Laptop”,15000、100,Baik,但实际输出为none。


慕村9548890
浏览 186回答 2
2回答

万千封印

我无法完全理解您的问题。但是根据我的理解,您需要这样的输出"Laptop",15000, 100, Baik<?php//function Multidimentional Arrayfunction 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 functionecho '"' . barang()[0]['nama_barang'] . '" ,' .&nbsp;barang()[0]['harga'] . ' ,' .&nbsp;barang()[0]['Qty'] . ' , ' .&nbsp;barang()[0]['Kondisi'];?>

呼唤远方

您可以用来array_map在浏览器上打印它们$charles =&nbsp;[["nama_barang"=>"Laptop","harga"=>15000,"Qty"=>100,"Kondisi"=>"Baik"],&nbsp;["nama_barang"=>"Dekstop","harga"=>30000,"Qty"=>30,"Kondisi"=>"Buruk"],["nama_barang"=>"Smartphone","harga"=>40000,"Qty"=>20,"Kondisi"=>"Baik Sekali"]];$res = array_map(function($v){&nbsp; &nbsp; return implode(',',$v); //echo implode(',',$v)}, $charles);echo '<pre>';print_r($res);
随时随地看视频慕课网APP
我要回答