PHP-获取并计数stdClass内部的所有内容

时间:2018-07-02 23:22:42

标签: php

这是我的数组:

Array ( 
   [0] => stdClass Object 
       ( [form] => Change Dormitory 
         [data] => stdClass Object 
                 ( [element] => input
                   [name] => form-start 
                   [class] => form-control mdate 
                   [type] => text 
                   [label] => Start ) 
           [0] => stdClass Object 
                 ( [element] => input 
                   [name] => form-end 
                   [class] => form-control mdate 
                   [type] => text 
                   [label] => End ) 
) 

我想计算[data]内的全部2,但我不知道如何获得它,并在foreach中使用它。我的尝试:

<?php
    $i=0;
    foreach ($param_form as $value) {
        print_r($value);
    $i++;
    }
?>

count((array)$param_form[$i]->data);

这是怎么做到的:

$form = array(
    array(
        'form' => 'Change Dormitory',
        'data' => array(
                        'element'=>'input',
                        'name'=>'form-start',
                        'class'=>'form-control mdate',
                        'type'=>'text',
                        'column'=>'col-md-4',
                        'label'=>'Start'
                    ),
                    array(
                        'element'=>'input',
                        'name'=>'form-end',
                        'class'=>'form-control mdate',
                        'type'=>'text',
                        'column'=>'col-md-4',
                        'label'=>'End'
                    ),

);

$new = json_encode($form);

$data = new stdClass;
$data->param_form = json_decode($new);

$this->load->view('my_view',$data);

3 个答案:

答案 0 :(得分:1)

我认为这是您要创建数组的方式

$form = array(
    array(
        'form' => 'Change Dormitory',
        'data' => array(
                    array(
                        'element'=>'input',
                        'name'=>'form-start',
                        'class'=>'form-control mdate',
                        'type'=>'text',
                        'column'=>'col-md-4',
                        'label'=>'Start'
                    ),
                    array(
                        'element'=>'input',
                        'name'=>'form-end',
                        'class'=>'form-control mdate',
                        'type'=>'text',
                        'column'=>'col-md-4',
                        'label'=>'End'
                    )
                ),

);

现在data属性将是一个包含2个子数组[0] and [1]的数组。

答案 1 :(得分:0)

首先,您需要确保数组正确$form。键data必须是要计数的数组,并且数组中包含项/数组。如果您要开始使用一个好的文本编辑器肯定会有所帮助。

喜欢

$form = array(
    array(
        'form' => 'Change Dormitory',
        'data' => array(
            array(),
            array(),
        ),
    ),
);

然后您可以通过以下方式计算数据中的项目 echo count($data->param_form[0]->data);

答案 2 :(得分:0)

def widespace(sentence: str, word: str, start: int = None, end: int = None):
    if not start:
        start = 0
    if not end:
        end = len(sentence)

    spacedword = " ".join(word)
    indices = []
    cur = sentence.find(word, start)

    # Find all occurrences of the word
    while cur >= 0 and cur < end + len(word) - 1:
        # Add to list
        indices.append(cur) 

        # Next occurrence
        cur = cur + len(word)
        cur = sentence.find(word, cur, end + len(word) - 1)
    print("\t" + str(indices)) # To check indices

    # Replace word with spaced-out word
    while len(indices) > 0:
        index = indices.pop()
        sa = sentence[:index]
        sb = sentence[index:index + len(word)]
        sc = sentence[index + len(word):]
        sb = sb.replace(word, spacedword)
        sentence = sa + sb + sc

    return sentence

计数:$form = array( 'form' => 'Change Dormitory', 'data' => array( array( 'element'=>'input', 'name'=>'form-start', 'class'=>'form-control mdate', 'type'=>'text', 'column'=>'col-md-4', 'label'=>'Start' ), array( 'element'=>'input', 'name'=>'form-end', 'class'=>'form-control mdate', 'type'=>'text', 'column'=>'col-md-4', 'label'=>'End' ), ), ); $data->param_form = $form; $this->load->view('my_view', $data);