2

I have an array like this which I fetch using file_get_content and here is other side url (http://sample.com/change.php)code from where I fetch array.

$a=array();
$a=Db::getInstance()->ExecuteS("SELECT *
FROM tablename
LIMIT 0 , 2
;");

$a=(array)$a;
print_r($a);

Then i use

$result = file_get_contents('http://sample.com/change.php');

That is the output of $result:

Array
(
    [0] => Array
        (
            [id_stock_available] => 1
            [id_product] => 1
            [id_product_attribute] => 0
            [id_shop] => 1
            [id_shop_group] => 0
            [quantity] => 3
            [depends_on_stock] => 0
            [out_of_stock] => 2
        )

    [1] => Array
        (
            [id_stock_available] => 2
            [id_product] => 2
            [id_product_attribute] => 0
            [id_shop] => 1
            [id_shop_group] => 0
            [quantity] => 1
            [depends_on_stock] => 0
            [out_of_stock] => 2
        )

)

When I apply foreach for $result:

foreach ($result as $value) {
    var_dump($value);
    //var_dump($value['installed'];
}

it shows me Invalid argument supplied for foreach().

11
  • try to check var_dump($result) first Commented Apr 29, 2014 at 8:28
  • It shows me output string(614) "Array ( [0] => Array ( [id_stock_available] => 1 [id_product] => 1 [id_product_attribute] => 0 [id_shop] => 1 [id_shop_group] => 0 [quantity] => 3 [depends_on_stock] => 0 [out_of_stock] => 2 ) [1] => Array ( [id_stock_available] => 2 [id_product] => 2 [id_product_attribute] => 0 [id_shop] => 1 [id_shop_group] => 0 [quantity] => 1 [depends_on_stock] => 0 [out_of_stock] => 2 ) ) " Commented Apr 29, 2014 at 8:44
  • have your tried this? $result = (array) $result; then do a var_dump again Commented Apr 29, 2014 at 8:56
  • 1
    if you look carefully, $result is actually a string. it says on your var_dump Commented Apr 29, 2014 at 9:01
  • @GianCarlo it shows me array(1) { [0]=> string(614) "Array ( [0] => Array ( [id_stock_available] => 1 [id_product] => 1 [id_product_attribute] => 0 [id_shop] => 1 [id_shop_group] => 0 [quantity] => 3 [depends_on_stock] => 0 [out_of_stock] => 2 ) [1] => Array ( [id_stock_available] => 2 [id_product] => 2 [id_product_attribute] => 0 [id_shop] => 1 [id_shop_group] => 0 [quantity] => 1 [depends_on_stock] => 0 [out_of_stock] => 2 ) ) " } now foreach invaid argument not showing but it is also showing correct array in foreach loop like am using foreach ($result as $csv) {} Commented Apr 29, 2014 at 9:10

3 Answers 3

1

On your php file you must change it to this:

// your query here
$a = Db::getInstance()->ExecuteS("SELECT * FROM tablename LIMIT 0 , 2;");
// then output it as JSON
header('Content-Type: application/json');
echo json_encode($a);

Then to get it on the other php:

$result = file_get_contents('http://sample.com/change.php');
$values = json_decode($result, true);

The values should be on $values as an array

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

2 Comments

Your welcome and no problem, so a tip: next time try to add and post relevant code, because this might be very helpful in solving the problem.
@user3450650 Also read-up on why singleton is bad practice: stackoverflow.com/questions/4595964/…
0

file_get_contents returns the content of response (return false on fail), which is a string, but not an array.

You have to parse the result of print_r back to array, which is not a good idea.

So use some encode/decode strategy to do this:

echo json_encode($a);

and

$result = file_get_contents('http://sample.com/change.php');
$result = json_decode($result, true);

Comments

0

print_r is not a proper way for serializing data for Web Services because serializing data means that it should be deserialized on the receiving end. As you know there's not a function that deserializes data outputted by print_r i.e. takes a string created by print_r and returning array that was passed in. Also from the docs:

print_r — Prints human-readable information about a variable

Human-readable doesn't mean computer-readable as well.

You have 2 alternatives:

  1. php's serialize/unserialize:

    // web service.
    $a=(array)$a;
    serialize($a);
    
    // receiving end.
    $result = file_get_contents('http://sample.com/change.php');
    var_dump(unserialize($result);
    
  2. json_encode/json_decode:

    // web service.
    $a=(array)$a;
    json_encode($a);
    
    // receiving end.
    $result = file_get_contents('http://sample.com/change.php');
    var_dump(json_decode($result);
    

I recommend using json because almost every platform can be client of your web service. While with native serializing, consumer of your WS will be client written in php also.

1 Comment

@user3450650 You're welcome. You need to mark answer which helped you with a tick on the left hand-side of answer.

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.