浏览并比较两个数组
Browse and compare two arrays
我需要比较一个来自查询的数组和一个基本数组,其中我有可能具有来自查询的数组的值,问题是如何遍历和比较这两个数组我在值不匹配的地方得到零并给我数组的值?。
这是我的代码:
public function age_range_men()
{
$type = 'brands';
$id_array= 75;
$date_start= '2017-01-02 00:01:00';
$date_end='2017-03-31 23:59:59';
$datos = $this->statistics_model->range_age_men($type, $id_array, $date_start, $date_end);
$result = array();
foreach($datos as $dato) {
$result[] = array((string)$dato->rango, (int)$dato->conexiones );
}
$base= array(array('12 a 17'), array('18 a 24'),array('25 a 34'), array('35 a 44'), array('45 a 54'),array('55 a 64'),array('mas de 65'));
$res = array();
foreach ($base as $base1) {
// print_r($base1[0]);
// echo "<br>";
foreach ($result as $result2){
if ($base1[0] == $result2[0]){
}else{
echo 0;
break;
}
}
}
}
[The first line is $result and the second line is $base][1]
[1]: https://i.stack.imgur.com/LC5Ps.jpg
这是我的建议。为结果创建关联数组。使 $base
成为一个键列表而不是一个奇怪的多维数组。检查数组中的键。我有 else echo 0 如果它存在我不知道你想要什么。
<?php
function age_range_men(){
$type = 'brands';
$id_array= 75;
$date_start= '2017-01-02 00:01:00';
$date_end='2017-03-31 23:59:59';
$datos = $this->statistics_model->range_age_men($type, $id_array, $date_start, $date_end);
$result = array();
foreach($datos as $dato) {
$result[(string)$dato->rango] = (int)$dato->conexiones; // Note this is now using a key
}
//This is no longer a multi-dimensional array just the keys we need
$base= array('12 a 17', '18 a 24','25 a 34', '35 a 44', '45 a 54','55 a 64','mas de 65');
$res = array();
foreach ($base as $key) {
if(array_key_exists($key, $results)){
//CODE???
} else {
echo 0;
}
}
}
age_range_men();
我需要比较一个来自查询的数组和一个基本数组,其中我有可能具有来自查询的数组的值,问题是如何遍历和比较这两个数组我在值不匹配的地方得到零并给我数组的值?。
这是我的代码:
public function age_range_men()
{
$type = 'brands';
$id_array= 75;
$date_start= '2017-01-02 00:01:00';
$date_end='2017-03-31 23:59:59';
$datos = $this->statistics_model->range_age_men($type, $id_array, $date_start, $date_end);
$result = array();
foreach($datos as $dato) {
$result[] = array((string)$dato->rango, (int)$dato->conexiones );
}
$base= array(array('12 a 17'), array('18 a 24'),array('25 a 34'), array('35 a 44'), array('45 a 54'),array('55 a 64'),array('mas de 65'));
$res = array();
foreach ($base as $base1) {
// print_r($base1[0]);
// echo "<br>";
foreach ($result as $result2){
if ($base1[0] == $result2[0]){
}else{
echo 0;
break;
}
}
}
}
[The first line is $result and the second line is $base][1]
[1]: https://i.stack.imgur.com/LC5Ps.jpg
这是我的建议。为结果创建关联数组。使 $base
成为一个键列表而不是一个奇怪的多维数组。检查数组中的键。我有 else echo 0 如果它存在我不知道你想要什么。
<?php
function age_range_men(){
$type = 'brands';
$id_array= 75;
$date_start= '2017-01-02 00:01:00';
$date_end='2017-03-31 23:59:59';
$datos = $this->statistics_model->range_age_men($type, $id_array, $date_start, $date_end);
$result = array();
foreach($datos as $dato) {
$result[(string)$dato->rango] = (int)$dato->conexiones; // Note this is now using a key
}
//This is no longer a multi-dimensional array just the keys we need
$base= array('12 a 17', '18 a 24','25 a 34', '35 a 44', '45 a 54','55 a 64','mas de 65');
$res = array();
foreach ($base as $key) {
if(array_key_exists($key, $results)){
//CODE???
} else {
echo 0;
}
}
}
age_range_men();