0

I have a very complicated array, and i want to convert it into a PHP valid array so i can loop through the values.

JS array:

$test = (       // Portraits
  {'image'=> 'http://farm8.staticflickr.com/7319/8993158058_f82968e61a_b.jpg', 'thumb'=> 'http://farm8.staticflickr.com/7319/8993158058_f82968e61a_t.jpg'},
  {'image'=> 'http://farm3.staticflickr.com/2891/8993155214_b8e091c625_b.jpg', 'thumb'=> 'http://farm3.staticflickr.com/2891/8993155214_b8e091c625_t.jpg'},
  {'image'=> 'http://farm8.staticflickr.com/7432/8993133146_d647438c55_b.jpg', 'thumb'=> 'http://farm8.staticflickr.com/7432/8993133146_d647438c55_t.jpg'});

EDIT

PHP loop: it should the image part for every value

  for ($i = 0;$i < $test.length;$i++){
    saveToDisk($test[$i]['image'],$i);
 }

Is the above correct? how can i read through the values?

2
  • 1
    if you swap { and } with [ and ] you hafve valid php short array notation, if you swap => with : it looks like valid JSON. Commented Nov 10, 2014 at 13:30
  • @coder101 take a look at my answer, it's a working example on how you should do it. just make sure you strinify your Javascript object in order to be able to use json_decode on it. Commented Nov 10, 2014 at 13:35

3 Answers 3

1

You can use json_decode function to convert json formatted string into a PHP arrays (or object if you're calling it without the second parameter or with false instead)

$test = '[{"image":"http:\/\/farm8.staticflickr.com\/7319\/8993158058_f82968e61a_b.jpg","thumb":"http:\/\/farm8.staticflickr.com\/7319\/8993158058_f82968e61a_t.jpg"},{"image":"http:\/\/farm3.staticflickr.com\/2891\/8993155214_b8e091c625_b.jpg","thumb":"http:\/\/farm3.staticflickr.com\/2891\/8993155214_b8e091c625_t.jpg"},{"image":"http:\/\/farm8.staticflickr.com\/7432\/8993133146_d647438c55_b.jpg","thumb":"http:\/\/farm8.staticflickr.com\/7432\/8993133146_d647438c55_t.jpg"}]';

$array = json_decode($test, 1);

// Looping each inner array and printing image/thumb keys
foreach ($array as $arr) {
   echo $arr['image'].' - '.$arr['thumb'];
}

Also in order to convert your JavaScript object to a valid JSON string you should be using the JSON.strngify function:

JSON.stringify(object); // <-- JavaScript function
Sign up to request clarification or add additional context in comments.

6 Comments

that string isn't quite json
@kris You are right, that's because he's example wasn't in the correct json string format. I've edited my answer to show my example.
I've edited the question, how can i read the values of the image part using the loop. Please help.
@coder101 , just make sure the string you are passing is using in valid JSON format (using the strinify function in JavaScript). and you will be able to use Json_decode to convert it into an PHP array.
your example doesn't really solve my problem. Can you show it with multiple values, also with the thumb part as i provided in the question?
|
0

you can read parse in PHP using json_decode

Comments

0

Your "array" code looks like an invalid mixture between PHP and Javascript syntax. It should look more like:

$test = json_decode("[
  {'image'=> 'http://farm8.staticflickr.com/7319/8993158058_f82968e61a_b.jpg', 'thumb'=> 'http://farm8.staticflickr.com/7319/8993158058_f82968e61a_t.jpg'},
  {'image'=> 'http://farm3.staticflickr.com/2891/8993155214_b8e091c625_b.jpg', 'thumb'=> 'http://farm3.staticflickr.com/2891/8993155214_b8e091c625_t.jpg'},
  {'image'=> 'http://farm8.staticflickr.com/7432/8993133146_d647438c55_b.jpg', 'thumb'=> 'http://farm8.staticflickr.com/7432/8993133146_d647438c55_t.jpg'}]");

Or better yet:

$test = [
  ['image'=> 'http://farm8.staticflickr.com/7319/8993158058_f82968e61a_b.jpg', 'thumb'=> 'http://farm8.staticflickr.com/7319/8993158058_f82968e61a_t.jpg'],
  ['image'=> 'http://farm3.staticflickr.com/2891/8993155214_b8e091c625_b.jpg', 'thumb'=> 'http://farm3.staticflickr.com/2891/8993155214_b8e091c625_t.jpg'],
  ['image'=> 'http://farm8.staticflickr.com/7432/8993133146_d647438c55_b.jpg', 'thumb'=> 'http://farm8.staticflickr.com/7432/8993133146_d647438c55_t.jpg']
];

2 Comments

How can i read the values through a for loop using the second version of the array syntax you provided?
@coder101 i've added a foreach statment in my 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.