0

I want to get data from an array in array inside of session. i want to get the rule1 of each array. But when i print $y like in the code below, it doesn't print anything

$this->session->userdata['namearray'];

this is print_r of the session

Array (
        [0] => stdClass Object
        (
            [class] =>array (
                   [0] => stdClass Object
                   (
                     [rule1] =>a

                    )
                   [1] => stdClass Object
                   (
                     [rule1] =>b

                    )
             )
         [1] => stdClass Object
        (
            [class] =>array (
                   [0] => stdClass Object
                   (
                     [rule1] =>c

                    )
                   [1] => stdClass Object
                   (
                     [rule1] =>d

                    )
             )

    )

foreach ($nameArray as $y){ 
                $string2[] = $y->class->rule1;  
             }

how to make the correct foreach

3
  • Please revert your previous question to original. _/\_ Commented May 20, 2020 at 3:58
  • done @sauhardnc Commented May 20, 2020 at 4:12
  • Does this answer your question? codeigniter pass array in session and retrieve Commented May 20, 2020 at 9:10

1 Answer 1

0

I've written a possible solution for your query, which is quite similar to your previous question. Comments are mentioned wherever necessary. See if it helps you.

$nameArray = $this->session->userdata('namearray'); // access the session data like this (remove namearray with your session name)

foreach ($nameArray as $x){ // traverse the data

    foreach($x->class as $class){ // now traverse the class array which is an object of parent array

        $newArr[][] = $class->rule1;  // use -> to access objects

    }
}

This will return an array like

// print_r($newArr);

[0] => 
      [0] => a
      [1] => b
[1] => 
      [0] => c
      [1] => d
Sign up to request clarification or add additional context in comments.

5 Comments

i want to make it into a two array @sauhardnc 0 a b 1 c d
What do you mean by two array? Like first array will have a and b, and second will have c and d Like so? @Loka
Can you share the desired output in the question?
inside the second foreach $string2[]=$class->rule1; and then $data = array( 'rule' =>$string2 ); $this->load->view('view',$data)
because in the view i want to make 2 div and in every div there is the name. first div is a and b and the second div is c and d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.