php 对多维数组的操作
我想将数据循环插入多维数组,请问该怎么做?
代码如下:
$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));/*----------如何将一个数据添加到数组中,使数组输出如下---------------------*//* Array ( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa[msg3] => dasdsa[msg4] => dasdsa ) ) */
我用了array_push函数,可是结果不是我想要的。
代码如下:
$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));array_push($a['date'],array('msg3'=>'tel'));print_r($a);
输出结果是
Array ( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [0] => Array ( [msg3] => tel ) ) )
但我想要的是 Array ( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [msg3] => tel ) )
请问如何才能做到?求大神帮忙。
急需!!!
回复讨论(解决方案)
$a=array();foreach($data as $k=>$v){ $a['date']['msg'.$k]=$v;}print_r($a);
$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));$a['date']['msg3'] = 'tel';print_r($a);
Array( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [msg3] => tel ))
$b = array('msg3'=>'tel');foreach($b as $k=>$v){ $a['date'][$k] = $v;}print_r($a);
$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));$a['date']['msg3'] = 'tel';print_r($a);
Array( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [msg3] => tel ) )
$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));$a['date'] = array_merge($a['date'],array('msg3'=>'tel'));print_r($a);
Array( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [msg3] => tel ))
$a=array("date"=>array("msg1"=>"asdasd","msg2"=>"dasdsa"));$a = array_merge_recursive($a, array('date' => array('msg3'=>'tel')));print_r($a);
Array( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [msg3] => tel ))
感谢大家帮忙!!
我将数组变为json
Array ( [date] => Array ( [msg1] => asdasd [msg2] => dasdsa [msg3] => tel [num] => 3 ) ){ "date": { "msg1":"asdasd", "msg2":"dasdsa", "msg3":"tel", "num":"3" }}
但是在js里如何取出json的数据??单个取出,比如说我想取出num。
alert(json.date.num)
你有点本末倒置了
一般的说,客户端的灵活性要比服务端的差
所以应先决定客户端怎么做、需要什么样的数据后,再由服务端组装
<script type="text/javascript"> var json = {"date":{"msg1":"asdasd","msg2":"dasdsa","msg3":"tel","num":"3"}}; alert(json.date.num);</script>
var obj = JSON.parse(s); //s为你的JSON串
alert(obj.date.num);
多谢了。
写的时候不太了解php数组和json,由于是依靠json_encode()函数,也不太清楚json结构会是什么样。
但是你给的还不是我想要的结果,还有一些问题。
json:
date后面还有日期,日期是一个变量。日期可以做到循环变化,不用管。
msg后面也有一个变量。
//我想这样写,但是不管用//以下代码不管用,但是我想取出每个日期的num,再取出msg。day="这里有变化的日期";var i=json["date"+day]["num"]; //这样无输出var i=json["date"+day].num; //这样也无输出if (i!=0){ for (var j=1;j<=i;j++) { alert(json["date"+day]["msg"+j]); //输出信息。这个也不行 }}
我想让输出结果为这样:(日期1:“msg1的内容”、“msg2的内容”…………),(日期2:“msg1的内容”、“msg2的内容”…………) …………不用管输出格式,只要得到每个日期的信息内容就行了。
请问该怎么写??
对了,得到的json已经转换了。
$.ajax({……………………………… success:function (data) { var c=data; var ss; json=eval("("+c+")"); }});
多谢大家,我的问题已解决。结贴。。。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
