2

I have an array of ways to retrieve paper objects? I try foreach,Trying to get property 'sample_url' of non-object,and Illegal string offset 'sample_url' , how get sample_url in object paper?

 "active_joins2": [
    {
      "join_registration_number": "SS-006672-I-ID",
      "join_status": "checked",
      "join_tag": "9#science#indonesia#Primary 3",
      "student_id": 7502,
       "paper": {
          "sample_id": 57,
          "sample_title": "Science Indonesia Primary 3",
          "sample_url":"http:google.com/abc/sample/1548989381.pdf",
          "sample_tag": "9#science#indonesia#Primary 3"
       }
    },
    {
      "join_registration_number": "MS-006687-I-ID",
      "join_status": "checked",
      "join_tag": "9#math#indonesia#Primary 3",
      "student_id": 7502,
      "paper": {
         "sample_id": 47,
         "sample_title": "Math Indonesia Primary 3",
         "sample_url":"http://google.com/sample/1548988991.pdf",
         "sample_tag": "9#math#indonesia#Primary 3"
       }
   }
]

in blade view

   @foreach ($item['active_joins2'] as $row => $val)
       @php 
          $string = str_replace('#',' ',$val['join_tag']);
          $exString = explode(" ",$string);
       @endphp       
       <a href="{{}}" style="text-decoration:none;">
           <div class="card jismo-practice-paper mx-auto mb-3">
               <div class="card-body row">
                    <div class="col-lg-6 col-xl-6 col-md-6 col-sm-6 col-6">
                                    <div class="subject" style='text-transform: capitalize'>{{$exString[1]}}</div>
                                    <div class="language" style='text-transform: capitalize'>{{$exString[2]}}</div>
                                </div>
                                <div class="col-lg-6 col-xl-6 col-md-6 col-sm-6 col-6">
                                    <div class="grade">{{$exString[3]}}</div>
                                    <div class="gradenumber">{{$exString[4]}}</div>
                                </div>
                            </div>  
                        </div>
                    </a>
                @endforeach

2
  • Code snippet is actually a JSON, do you work with JSON or with php array? Commented Dec 4, 2019 at 15:11
  • This has already been made an array Commented Dec 4, 2019 at 15:15

2 Answers 2

2

Do simply with json_decode(), Also your json string is missing starting and ending curly braces i.e { }

$array = json_decode($object,1);
foreach($array['active_joins2'] as $key=>$value){
    echo $value['paper']['sample_url'].PHP_EOL;
}

WORKING DEMO: https://3v4l.org/AbsLS

Sign up to request clarification or add additional context in comments.

2 Comments

active_joins2 is an array not an object
@MuhamadRahmatSetiawan did you check my working demo?
1

I think this is json, you can use json_decode function and after that use that in php like array or std object json_decode

Comments

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.