0

I Select * From my sql table in PHP, and I convert it to JSON, so the results are like this:

([
    {"id":"350","Name":"BlaBla","Info":"BlaBla"}, 
    {"id":"351","Name":"BlaBla","Info":"BlaBla"},
    {"id":"352","Name":"BlaBla","Info":"BlaBla"}
]);

I scrape a clients website for images (this is a request of the client) based on the id in the records above, and I output images into a similar array/dictionary and output into JSON:

([
    {"image1":"http://Sourceofimg.jpg"},
    {"image2":"http://Sourceofimg.jpg"},
    {"image3":"http://Sourceofimg.jpg"},
    {"image4":"http://Sourceofimg.jpg"},
    {"image5":"http://Sourceofimg.jpg"}
]);

So lets say I scrape a page, page.php?id=350, I'd get a similar output to the image array above, how can I append/add that result to the first array where id=350?

EDIT

This is where I would like to combine the arrays:

while ($row = mysql_fetch_assoc($results)) 
                    { 
    $rows[] = $row;
    $url = 'http://www.url.com/page.php?id='.$row['id'].'';
    $html = file_get_html($url);
    foreach($html->find('div.classvalue') as $element){
    foreach($element->find('img') as $img){

      $images[] = array("image".$i."" => $img->src);
      $i = $i + 1;
      $rows = array_merge($rows, $images);

} } }

My version clearly does not work, it seems to be appending new images to the already existing image[] therefore the last element of $rows[] will get the full list of images, where I just want the images tied in with that id. Also by merging, it does not merge correctly I get an output like this e.g:

([
    {"id":"350","Name":"BlaBla","Info":"BlaBla"},
    {"image1":"http://Sourceofimg.jpg"},
    {"image2":"http://Sourceofimg.jpg"}, 
    {"id":"351","Name":"BlaBla","Info":"BlaBla"}
]);

I would like it like:

([
    {"id":"350","Name":"BlaBla","Info":"BlaBla", "image1":"http://Sourceofimg.jpg", "image2":"http://Sourceofimg.jpg"} etc...
]);
6
  • 1
    don't do it to the json string. do it to whatever structure you used to CREATE the json - e.g. don't convert to json until you're done. stay with native php arrays/objects until right before the end. Commented May 1, 2013 at 14:14
  • That's what I'm doing, converting to JSON is the last line of my file. I'm trying to do this in the while($row = mysql_fetch_assoc($results)){}. $rows[] is my table data $images[] is my images (obvious), when i pass a query manually It's fine, but in this loop it's incorrect Commented May 1, 2013 at 14:21
  • where is the closing } for the first for loop? Commented May 1, 2013 at 14:39
  • } = not a problem, I just did not copy and paste it on here, but I'll do so now. Commented May 1, 2013 at 14:40
  • @MikeJ There's actually two closing } missing. Please check your code because you really need to close a for loop after you've opened it... Commented May 1, 2013 at 14:42

3 Answers 3

1

I think this is what you need. You first have to start with a clean $images array using $images = array(); so you won't have the results of previous loops in your array. Then you should collect the images inside the $images array (using the forloops). Then you can store the images in the $row array under the key 'images' using $row['images'] = $images;.

Hope this is what you need.

while ($row = mysql_fetch_assoc($results)) { 

    $url = 'http://www.url.com/page.php?id='.$row['id'].'';
    $html = file_get_html($url);
    $images = array();
    $i = 0;

    foreach($html->find('div.classvalue') as $element){

        foreach($element->find('img') as $img){
            $row["image".$i] = $img->src;    
            $i = $i + 1;    
        }

    }

    $rows[] = $row;

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

2 Comments

It's really getting close now, needs 1 more fix. The results I get now is: ([{"id":350","Name":"BlaBla","Info":"BlaBla","images":{[{"image1":"imgsource"}]}) is it possible to have the images stored within the record dictionary?
I changed the code. Note that I now also added $i = 0;, otherwise the counter for your images will not be reset for each row.
0

On php side use array_merge() with both arrays. In addition array_unique will become handy (it will remove duplicates).

1 Comment

That will just merge both, I want to add the results of the image array to a specific row in the first array.
0

You have to add $images to the actual row. What you are doing, is resizing array.

$actualIndex = count($rows);
$rows[] = $row;
...
...
$rows[$actualIndex] = $images; 

Or something like this

You can tweak $rows[$actualIndex] to satisfy your structure. First set somewhere $imageID = 1;

$rows[$actualIndex]['image'.$imageID] = $images['url']
imageID++;

1 Comment

Tried this, but not quite there, the results I get are: ({"0":{"id":350, "Name":"BlaBla","Info":"BlaBla"},"17":[{"image1":"http://Sourceofimg.jpg"}]});

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.