0

my code so far

<?php
foreach($sub_category as $row2){

    $url = 'https://myurl.com';  
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);     
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $data = curl_exec($ch);
    curl_close($ch);
   
    echo $name." data = "."<pre>"; print_r($data);
}


?>

This is $data output

Array
(
    [0] => stdClass Object
        (
            [course_title] => 60+ Space Facts To Get An A In Astronomy
            [description] =>  60+ Space Facts To Get An "A" In Astronomy
            [image] => pexels-pixabay-39896_20210220014840929292735.jpg
            
        )

    [1] => stdClass Object
        (
            
            [course_title] => Learn Chinese
            [description] => Learn Chinese
            [image] => Chinoabc_20211227114307692406780.png
            
        )
 )

I want to convert it to look like below

Array
        (
            [0] => array
                (
                    [course_title] => 60+ Space Facts To Get An A In Astronomy
                    [description] =>  60+ Space Facts To Get An "A" In Astronomy
                    [image] => pexels-pixabay-39896_20210220014840929292735.jpg
                    
                )
        
            [1] => array
                (
                    
                    [course_title] => Learn Chinese
                    [description] => Learn Chinese
                    [image] => Chinoabc_20211227114307692406780.png
                    
                )
         )

i have tried many answers from here converting array of objects to simple array but it's not working for me

i have tried many answers from already posted questions. my problem is different than any other relevant question posted here

3
  • 1
    When you enable CURLOPT_RETURNTRANSFER then curl_exec() returns a string. Unless I'm missing something, $data is not an array of objects, it's just a text that happens to contain a PHP variable dump for some reason. It may be more obvious if you replace print_r() with var_dump(). Commented Jan 10, 2022 at 18:15
  • Are you sure you didn't write $data = json_decode(curl_exec($ch));? Commented Jan 10, 2022 at 19:56
  • APIs don't usually return print_r() output, they usually return JSON. Commented Jan 10, 2022 at 19:57

1 Answer 1

2

You can use the (array) type cast to convert an object to an equivalent associative array.

$data = array_map(function($x) { return (array)$x; }, $data);

If the original array of objects came from using json_decode(), you can tell it to return associative arrays instead of objects by giving it a true second argument.

$data = json_decode($data, true);
Sign up to request clarification or add additional context in comments.

5 Comments

tried but not working for me the array #1 is stored inside $data var, i use array_map function $x = $data right ?, i am getting 2 errors 1= Message: Undefined variable: array & 2= Message: array_map(): Argument #2 should be an array
$array is your array.
I've updated the answer to use your variable.
good finding you were correct the data was not json object that's why not working as array but simple string now in source i use $data =json_encode($data,true); than used your $data = json_decode($data, true); and yes done now i have string as an array ready to use thumbs up :)

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.