简体   繁体   English

如何使用PHP在JSON数组中打印第1,第3,第5个元素

[英]How to print 1st,3nd,5th elements in JSON array using PHP

I triied to get data from other web site and convert it in to json array. 我试图从其他网站获取数据并将其转换为json数组。

following code used for it. 以下代码用于它。

foreach($models1 as $mod)
{
    $result[] = array('model'=> $mod);

}

$finalResultArray = array(
        'mobiles' => $result
);

echo json_encode($finalResultArray);

then the result was 然后结果是

{"mobiles":[{"model":"Apple iPhone 5c 16GB"},{"model":"93,000"},{"model":"Apple iPhone 5s 16GB"},{"model":"117,312"},{"model":"Apple iPhone 5 32GB"},{"model":"94,280"},{"model":"Apple iPhone 4S 16GB"},{"model":"82,000"},{"model":"Apple iPhone 5 16GB"},{"model":"93,000"}]} {“ mobiles”:[{“ model”:“ Apple iPhone 5c 16GB”},{“ model”:“ 93,000”},{“ model”:“ Apple iPhone 5s 16GB”},{“ model”:“ 117,312” },{“ model”:“ Apple iPhone 5 32GB”},{“ model”:“ 94,280”},{“ model”:“ Apple iPhone 4S 16GB”},{“ model”:“ 82,000”},{“型号”:“ Apple iPhone 5 16GB”},{“型号”:“ 93,000”}]}

I want to print this array as like this 我想像这样打印此数组

{"mobiles":[{"model":"Apple iPhone 5c 16GB"},{"model":"Apple iPhone 5s 16GB"},{"model":"Apple iPhone 5 32GB"},{"model":"Apple iPhone 4S 16GB"},{"model":"Apple iPhone 5 16GB"}]} {“ mobiles”:[{“ model”:“ Apple iPhone 5c 16GB”},{“ model”:“ Apple iPhone 5s 16GB”},{“ model”:“ Apple iPhone 5 32GB”},{“ model”: “ Apple iPhone 4S 16GB”},{“型号”:“ Apple iPhone 5 16GB”}]}

i want to remove {"model":"93,000"},{"model":"117,312"} etc... from this JSON array 我想从此JSON数组中删除{“ model”:“ 93,000”},{“ model”:“ 117,312”}等...

Can any one help me...???? 谁能帮我...????

As it has been seen in your question , you want to remove the odd index number array values. 正如您在问题中所看到的,您想要删除奇数索引号数组值。

$i = 0;
foreach($models1 as $mod)
{
    if(($i % 2) == 0) {
        $result[] = array('model'=> $mod);
        $i++;
    }
}

If you can change the way you make the array, it will be simple enough to do using a modulus operator as follows: 如果您可以更改制作数组的方式,则使用模运算符将非常简单,如下所示:

$i=0;
foreach($models1 as $mod)
{
    if(($i+1)%2==1)
    {
        $result[] = array('model'=> $mod);
    }
    $i++;
}

This will only insert the odd numbers into the array to start off with. 这只会将奇数插入数组开始。 If you want to have all the entries in the array but only display the odd ones, you can easily modify this example to output only what you need. 如果要在数组中包含所有条目,但仅显示奇数条目,则可以轻松修改此示例以仅输出所需的内容。

上面提供的解决方案实际上很好,尽管它具有array_filter函数,所以您可以轻松地进行复杂的过滤,而不仅仅是“奇数模型”。

You can do it like this convert json to array, skip the odd values, and store in an array and again convert array to json 您可以像这样将json转换为数组,跳过奇数,然后存储在数组中,然后再次将数组转换为json

    foreach($models1 as $key => $value)
    {
        if($key%2 == 0)
        {
            $arr_new['mobiles'][] = $value;
        }
    }
    $json_new = json_encode($arr_new);
    echo $json_new;

The output is as below 输出如下

       {"mobiles":[{"model":"Apple iPhone 5c 16GB"},{"model":"Apple iPhone 5s 16GB"},{"model":"Apple iPhone 5 32GB"},{"model":"Apple iPhone 4S 16GB"},{"model":"Apple iPhone 5 16GB"}]}

If the rule is just to remove elements with odd keys then try this: 如果规则只是删除带有奇数键的元素,请尝试以下操作:

// This loop keeps elements with index 0, 2, 4 ... and so on
for ($i=0;$i<count($models1);$i=$i+2){
  $finalResultArray['mobiles'][] = array('model' => $models1[$i]);
} 

echo json_encode($finalResultArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM