0

I'm pretty sure I'm quite close so hopefully quick answer.

I'm able to generate this JSON:

apps: [
    {
        0: {
            PublisherCount: "7"
    },
        Id: "87",
        AppName: "Productivity, Focus, Habits & Life Success by Audiojoy",
        AppBundle: "productivitymind"
    }
]

But I'm trying to get to:

apps: [
    {
        Id: "87",
        AppName: "Productivity, Focus, Habits & Life Success by Audiojoy",
        AppBundle: "productivitymind",
        PublisherCount: "7"
    }
]

Here is my output loop (I think the issue is in the 5th row where I array_push the new value for PublisherCount. It creates an additional node instead of adding it to the end.

$temp_array = array();
$i = 0;
while ($row = mysqli_fetch_assoc($publisher_apps)) {
$temp_array[] = $row;
$temp_array[$i][] = fetch_all(get_publisher_count_by_app_id($row['Id']))[0];
$i++;
}

$publisher_apps = $temp_array;

$result = array("apps"=>$publisher_apps);

output_json($result);

Thanks.

1
  • hum, try $temp_array[$i]['PublisherCount'] = fetch_all(get_publisher_count_by_app_id($row['Id']))[0]['PublisherCount']; (and indent your code) Commented Feb 13, 2017 at 23:33

2 Answers 2

1

You have rows like this:

['Id' => "87",
 'AppName' => "Productivity, Focus, Habits & Life Success by Audiojoy",
 'AppBundle' => "productivitymind"]

and fetch_all(get_publisher_count_by_app_id($row['Id']))[0] returns an array like this:

['PublisherCount' => 7]

so when you append it with $temp_array[$i][], the entire array gets assigned to the 0 key of $temp_array[$i].

There are various different ways you could get just the PublisherCount value. One way is to use array_merge to combine the result of get_publisher_count_by_app_id with $row, and then add the modified $row to your main array.

while ($row = mysqli_fetch_assoc($publisher_apps)) {
    $count = fetch_all(get_publisher_count_by_app_id($row['Id']))[0];
    $temp_array[] = array_merge($row, $count);
}

If you do it this way, $i should become unneccessary.

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

1 Comment

Yup! @Dont' Panic that worked. Thank you. Makes sense, i kept getting a separate key for it.
0

Change it to this:

$temp_array = array();

while ($row = mysqli_fetch_assoc($publisher_apps)) {
  $row['PublisherCount'] = fetch_all(get_publisher_count_by_app_id($row['Id']))[0]['Pub‌​lisherCount'];
  $temp_array[] = $row;
}

$publisher_apps = $temp_array;

$result = array("apps"=>$publisher_apps);

output_json($result);

2 Comments

this results in this: apps: [ { Id: "87", AppName: "Productivity, Focus, Habits & Life Success by Audiojoy", AppBundle: "productivitymind", }, { PublisherCount: { PublisherCount: "12" } } ]
My bad, I've updated my answer. Please the updated code instead. However, after you get it working, rather than using a "fetch_all" function you should be looking to use a "fetch_one" or "fetch_first" kind of function, if one exists in your codebase, for performance purposes.

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.