Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Array and Sub Array from PHP

Array and Sub Array from PHP

Scheduled Pinned Locked Moved Web Development
phpsharepointdatabasemysqlcom
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I have the following SQL:

    SELECT customers_address.customer_address_id, address_category.address_category_name, customers_address.address_name,
    address_details.address_details_id,
    address_details.address_details_text
    FROM customers_address
    JOIN address_category ON address_category.address_category_id = customers_address.address_category_id
    LEFT JOIN address_details ON address_details.customer_address_id = customers_address.customer_address_id
    WHERE customer_id = param_customer;

    I want to show this output in JSON:

    CustomerAddress:
    {
    customer_address_id
    address_category_name
    address_name
    }
    Details:
    {
    address_details_id
    address_details_text
    }

    my current php is:

    $mysql_query = $mysql_connection->prepare('CALL sp_populate_customer_addresses(:param_customer)');
    $mysql_query->bindParam(':param_customer', $customer_id, PDO::PARAM_STR);
    $mysql_query->execute();

    if ($mysql_query->rowCount() <= 0) { echo "false"; }
    else
    {
    $jsonData = array();

    while($mysql_row = $mysql_query->fetch())
    {
    $jsonData[] = $mysql_row;
    }

    echo json\_encode($jsonData);
    

    }

    Thanks, [Jassim](https://www.softnames.com)

    Technology News @ www.JassimRahma.com

    P 1 Reply Last reply
    0
    • J Jassim Rahma

      Hi, I have the following SQL:

      SELECT customers_address.customer_address_id, address_category.address_category_name, customers_address.address_name,
      address_details.address_details_id,
      address_details.address_details_text
      FROM customers_address
      JOIN address_category ON address_category.address_category_id = customers_address.address_category_id
      LEFT JOIN address_details ON address_details.customer_address_id = customers_address.customer_address_id
      WHERE customer_id = param_customer;

      I want to show this output in JSON:

      CustomerAddress:
      {
      customer_address_id
      address_category_name
      address_name
      }
      Details:
      {
      address_details_id
      address_details_text
      }

      my current php is:

      $mysql_query = $mysql_connection->prepare('CALL sp_populate_customer_addresses(:param_customer)');
      $mysql_query->bindParam(':param_customer', $customer_id, PDO::PARAM_STR);
      $mysql_query->execute();

      if ($mysql_query->rowCount() <= 0) { echo "false"; }
      else
      {
      $jsonData = array();

      while($mysql_row = $mysql_query->fetch())
      {
      $jsonData[] = $mysql_row;
      }

      echo json\_encode($jsonData);
      

      }

      Thanks, [Jassim](https://www.softnames.com)

      Technology News @ www.JassimRahma.com

      P Offline
      P Offline
      Planet Thomas
      wrote on last edited by
      #2

      It is quite simple. You should change :

      $jsonData[] = $mysql_row;

      to something like this

      $jsonData[$i]['customer_address_id'] = $mysql_row['customer_address_id];
      itd ..
      $jsonData[$i]['Details']['address_details_id'] = $mysql_row['address_details_id];

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups