За последние 24 часа нас посетил 22501 программист и 1188 роботов. Сейчас ищут 757 программистов ...

json_decode как вытащить значение телефона?

Тема в разделе "Прочие вопросы по PHP", создана пользователем pix3x, 26 фев 2021.

  1. pix3x

    pix3x Новичок

    С нами с:
    26 фев 2021
    Сообщения:
    8
    Симпатии:
    0
    Привет,

    Есть база клиентов в ZCRM: https://prnt.sc/107gyys

    Пытаюсь получить из Json номера телефонов (+74959999999 и +71234567890), но массив сложный, никак не могу разобраться. Статус (success) в конце получается, а с номера ми телефонов беда. Код
    PHP:
    1. $answerObject = json_decode($result);
    2. print_r($answerObject);
    3. print_r($answerObject->status);
    выдаёт такой массив:
    stdClass Object ( [status] => success [data] => stdClass Object ( [customers] => Array ( [0] => stdClass Object ( [id] => 11014531 [subdomain_id] => 16086 [name] => Raz [status] => company [type] => potential [responsible_user_id] => [employees_count] => 50 [comment] => [country] => RU [city] => Moscow [address] => [zip] => [website] => => [skype] => [created_at] => 2020-09-17 21:25:50 [created_by] => 75006 [lead_status] => not_processed [lead_source] => manual [lead_created_at] => [lead_created_by] => [import_id] => 6819 [lead_called_to] => [phones] => Array ( [0] => stdClass Object ( [id] => 6489647 [phone] => +74954551111 [type] => work ) ) [contacts] => Array ( ) [labels] => Array ( [0] => stdClass Object ( [id] => 103731 [label] => razamanaz ) ) ) [1] => stdClass Object ( [id] => 11014532 [subdomain_id] => 16086 [name] => Raz Saint Petersburg [status] => company [type] => potential [responsible_user_id] => [employees_count] => 50 [comment] => [country] => RU [city] => Saint Petersburg [address] => [zip] => [website] => [email] => [skype] => [created_at] => 2020-09-17 21:25:50 [created_by] => 75006 [lead_status] => not_processed [lead_source] => manual [lead_created_at] => [lead_created_by] => [import_id] => 6819 [lead_called_to] => [phones] => Array ( [0] => stdClass Object ( [id] => 6489648 [phone] => +78124551111 [type] => work ) ) [contacts] => Array ( ) [labels] => Array ( [0] => stdClass Object ( [id] => 103731 [label] => razamanaz ) ) ) [2] => stdClass Object ( [id] => 16382810 [subdomain_id] => 16086 [name] => Customer 1 [status] => individual [type] => potential [responsible_user_id] => 75006 [employees_count] => 50 [comment] => [country] => RU [city] => Санкт-Петербург [address] => [zip] => [website] => [email] => [skype] => [created_at] => 2021-02-25 20:23:10 [created_by] => 75006 [lead_status] => not_processed [lead_source] => manual [lead_created_at] => [lead_created_by] => [import_id] => [lead_called_to] => [phones] => Array ( [0] => stdClass Object ( [id] => 10581689 [phone] => +71234567890 [type] => work ) ) [contacts] => Array ( ) [labels] => Array ( [0] => stdClass Object ( [id] => 103731 [label] => razamanaz ) ) ) [3] => stdClass Object ( [id] => 16383155 [subdomain_id] => 16086 [name] => Customer 2 [status] => individual [type] => potential [responsible_user_id] => 75006 [employees_count] => 50 [comment] => [country] => RU [city] => Санкт-Петербург [address] => [zip] => [website] => [email] => [skype] => [created_at] => 2021-02-25 21:13:56 [created_by] => 75006 [lead_status] => not_processed [lead_source] => manual [lead_created_at] => [lead_created_by] => [import_id] => [lead_called_to] => [phones] => Array ( [0] => stdClass Object ( [id] => 10582073 [phone] => +74959999999 [type] => work ) ) [contacts] => Array ( ) [labels] => Array ( ) ) ) [totalCount] => 4 ) [sandbox] => 1 ) success
     
  2. pix3x

    pix3x Новичок

    С нами с:
    26 фев 2021
    Сообщения:
    8
    Симпатии:
    0
    сейчас такой выдаёт массив:
    success [data] => stdClass Object ( [customers] => Array ( [0] => stdClass Object ( [id] => 16382810 [subdomain_id] => 16086 [name] => Customer 1 [status] => individual [type] => potential [responsible_user_id] => 75006 [employees_count] => 50 [comment] => [country] => RU [city] => Санкт-Петербург [address] => [zip] => [website] => => [skype] => [created_at] => 2021-02-25 20:23:10 [created_by] => 75006 [lead_status] => not_processed [lead_source] => manual [lead_created_at] => [lead_created_by] => [import_id] => [lead_called_to] => [phones] => Array ( [0] => stdClass Object ( [id] => 10581689 [phone] => +71234567890 [type] => work ) ) [contacts] => Array ( ) [labels] => Array ( [0] => stdClass Object ( [id] => 103731 [label] => razamanaz ) ) ) [1] => stdClass Object ( [id] => 16383155 [subdomain_id] => 16086 [name] => Customer 2 [status] => individual [type] => potential [responsible_user_id] => 75006 [employees_count] => 50 [comment] => [country] => RU [city] => Санкт-Петербург [address] => [zip] => [website] => [email] => [skype] => [created_at] => 2021-02-25 21:13:56 [created_by] => 75006 [lead_status] => not_processed [lead_source] => manual [lead_created_at] => [lead_created_by] => [import_id] => [lead_called_to] => [phones] => Array ( [0] => stdClass Object ( [id] => 10582073 [phone] => +74959999999 [type] => work ) ) [contacts] => Array ( ) [labels] => Array ( ) ) ) [totalCount] => 2 ) [sandbox] => 1 ) success
     
  3. Drunkenmunky

    Drunkenmunky Активный пользователь

    С нами с:
    12 авг 2020
    Сообщения:
    1.476
    Симпатии:
    281
    Перед print_r() выведите <pre>
     
  4. pix3x

    pix3x Новичок

    С нами с:
    26 фев 2021
    Сообщения:
    8
    Симпатии:
    0
    Спасибо! Всё равно не понятно как phone достать...

    Код (Text):
    1.  success
    2.     [data] => stdClass Object
    3.         (
    4.             [customers] => Array
    5.                 (
    6.                     [0] => stdClass Object
    7.                         (
    8.                             [id] => 16382810
    9.                             [subdomain_id] => 16086
    10.                             [name] => Customer 1
    11.                             [status] => individual
    12.                             [type] => potential
    13.                             [responsible_user_id] => 75006
    14.                             [employees_count] => 50
    15.                             [comment] =>
    16.                             [country] => RU
    17.                             [city] => Санкт-Петербург
    18.                             [address] =>
    19.                             [zip] =>
    20.                             [website] =>
    21.                             [email] =>
    22.                             [skype] =>
    23.                             [created_at] => 2021-02-25 20:23:10
    24.                             [created_by] => 75006
    25.                             [lead_status] => not_processed
    26.                             [lead_source] => manual
    27.                             [lead_created_at] =>
    28.                             [lead_created_by] =>
    29.                             [import_id] =>
    30.                             [lead_called_to] =>
    31.                             [phones] => Array
    32.                                 (
    33.                                     [0] => stdClass Object
    34.                                         (
    35.                                             [id] => 10581689
    36.                                             [phone] => +71234567890
    37.                                             [type] => work
    38.                                         )
    39.  
    40.                                 )
    41.  
    42.                             [contacts] => Array
    43.                                 (
    44.                                 )
    45.  
    46.                             [labels] => Array
    47.                                 (
    48.                                     [0] => stdClass Object
    49.                                         (
    50.                                             [id] => 103731
    51.                                             [label] => razamanaz
    52.                                         )
    53.  
    54.                                 )
    55.  
    56.                         )
    57.  
    58.                     [1] => stdClass Object
    59.                         (
    60.                             [id] => 16383155
    61.                             [subdomain_id] => 16086
    62.                             [name] => Customer 2
    63.                             [status] => individual
    64.                             [type] => potential
    65.                             [responsible_user_id] => 75006
    66.                             [employees_count] => 50
    67.                             [comment] =>
    68.                             [country] => RU
    69.                             [city] => Санкт-Петербург
    70.                             [address] =>
    71.                             [zip] =>
    72.                             [website] =>
    73.                             [email] =>
    74.                             [skype] =>
    75.                             [created_at] => 2021-02-25 21:13:56
    76.                             [created_by] => 75006
    77.                             [lead_status] => not_processed
    78.                             [lead_source] => manual
    79.                             [lead_created_at] =>
    80.                             [lead_created_by] =>
    81.                             [import_id] =>
    82.                             [lead_called_to] =>
    83.                             [phones] => Array
    84.                                 (
    85.                                     [0] => stdClass Object
    86.                                         (
    87.                                             [id] => 10582073
    88.                                             [phone] => +74959999999
    89.                                             [type] => work
    90.                                         )
    91.  
    92.                                 )
    93.  
    94.                             [contacts] => Array
    95.                                 (
    96.                                 )
    97.  
    98.                             [labels] => Array
    99.                                 (
    100.                                 )
    101.  
    102.                         )
    103.  
    104.                 )
    105.  
    106.             [totalCount] => 2
    107.         )
    108.  
    109.     [sandbox] => 1
    110. )
     
  5. Drunkenmunky

    Drunkenmunky Активный пользователь

    С нами с:
    12 авг 2020
    Сообщения:
    1.476
    Симпатии:
    281
    Потому что вы декодируете json в объект, а не массив.
    json_decode($result, true);// вот так в массив
     
  6. pix3x

    pix3x Новичок

    С нами с:
    26 фев 2021
    Сообщения:
    8
    Симпатии:
    0
    Поменял, а как из массива телефон взять?

    Код (Text):
    1. Array
    2. (
    3.     [status] => success
    4.     [data] => Array
    5.         (
    6.             [customers] => Array
    7.                 (
    8.                     [0] => Array
    9.                         (
    10.                             [id] => 11014531
    11.                             [subdomain_id] => 16086
    12.                             [name] => Razamanaz
    13.                             [status] => company
    14.                             [type] => potential
    15.                             [responsible_user_id] =>
    16.                             [employees_count] => 50
    17.                             [comment] =>
    18.                             [country] => RU
    19.                             [city] => Moscow
    20.                             [address] =>
    21.                             [zip] =>
    22.                             [website] =>
    23.                             [email] =>
    24.                             [skype] =>
    25.                             [created_at] => 2020-09-17 21:25:50
    26.                             [created_by] => 75006
    27.                             [lead_status] => not_processed
    28.                             [lead_source] => manual
    29.                             [lead_created_at] =>
    30.                             [lead_created_by] =>
    31.                             [import_id] => 6819
    32.                             [lead_called_to] =>
    33.                             [phones] => Array
    34.                                 (
    35.                                     [0] => Array
    36.                                         (
    37.                                             [id] => 6489647
    38.                                             [phone] => +74954551111
    39.                                             [type] => work
    40.                                         )
    41.  
    42.                                 )
    43.  
    44.                             [contacts] => Array
    45.                                 (
    46.                                 )
    47.  
    48.                             [labels] => Array
    49.                                 (
    50.                                     [0] => Array
    51.                                         (
    52.                                             [id] => 103731
    53.                                             [label] => razamanaz
    54.                                         )
    55.  
    56.                                 )
    57.  
    58.                         )
    59.  
    60.                     [1] => Array
    61.                         (
    62.                             [id] => 11014532
    63.                             [subdomain_id] => 16086
    64.                             [name] => Razamanaz Saint Petersburg
    65.                             [status] => company
    66.                             [type] => potential
    67.                             [responsible_user_id] =>
    68.                             [employees_count] => 50
    69.                             [comment] =>
    70.                             [country] => RU
    71.                             [city] => Saint Petersburg
    72.                             [address] =>
    73.                             [zip] =>
    74.                             [website] =>
    75.                             [email] =>
    76.                             [skype] =>
    77.                             [created_at] => 2020-09-17 21:25:50
    78.                             [created_by] => 75006
    79.                             [lead_status] => not_processed
    80.                             [lead_source] => manual
    81.                             [lead_created_at] =>
    82.                             [lead_created_by] =>
    83.                             [import_id] => 6819
    84.                             [lead_called_to] =>
    85.                             [phones] => Array
    86.                                 (
    87.                                     [0] => Array
    88.                                         (
    89.                                             [id] => 6489648
    90.                                             [phone] => +78124551111
    91.                                             [type] => work
    92.                                         )
    93.  
    94.                                 )
    95.  
    96.                             [contacts] => Array
    97.                                 (
    98.                                 )
    99.  
    100.                             [labels] => Array
    101.                                 (
    102.                                     [0] => Array
    103.                                         (
    104.                                             [id] => 103731
    105.                                             [label] => razamanaz
    106.                                         )
    107.  
    108.                                 )
    109.  
    110.                         )
    111.  
    112.                     [2] => Array
    113.                         (
    114.                             [id] => 16382810
    115.                             [subdomain_id] => 16086
    116.                             [name] => Customer 1
    117.                             [status] => individual
    118.                             [type] => potential
    119.                             [responsible_user_id] => 75006
    120.                             [employees_count] => 50
    121.                             [comment] =>
    122.                             [country] => RU
    123.                             [city] => Санкт-Петербург
    124.                             [address] =>
    125.                             [zip] =>
    126.                             [website] =>
    127.                             [email] =>
    128.                             [skype] =>
    129.                             [created_at] => 2021-02-25 20:23:10
    130.                             [created_by] => 75006
    131.                             [lead_status] => not_processed
    132.                             [lead_source] => manual
    133.                             [lead_created_at] =>
    134.                             [lead_created_by] =>
    135.                             [import_id] =>
    136.                             [lead_called_to] =>
    137.                             [phones] => Array
    138.                                 (
    139.                                     [0] => Array
    140.                                         (
    141.                                             [id] => 10581689
    142.                                             [phone] => +71234567890
    143.                                             [type] => work
    144.                                         )
    145.  
    146.                                 )
    147.  
    148.                             [contacts] => Array
    149.                                 (
    150.                                 )
    151.  
    152.                             [labels] => Array
    153.                                 (
    154.                                     [0] => Array
    155.                                         (
    156.                                             [id] => 103731
    157.                                             [label] => razamanaz
    158.                                         )
    159.  
    160.                                 )
    161.  
    162.                         )
    163.  
    164.                     [3] => Array
    165.                         (
    166.                             [id] => 16383155
    167.                             [subdomain_id] => 16086
    168.                             [name] => Customer 2
    169.                             [status] => individual
    170.                             [type] => potential
    171.                             [responsible_user_id] => 75006
    172.                             [employees_count] => 50
    173.                             [comment] =>
    174.                             [country] => RU
    175.                             [city] => Санкт-Петербург
    176.                             [address] =>
    177.                             [zip] =>
    178.                             [website] =>
    179.                             [email] =>
    180.                             [skype] =>
    181.                             [created_at] => 2021-02-25 21:13:56
    182.                             [created_by] => 75006
    183.                             [lead_status] => not_processed
    184.                             [lead_source] => manual
    185.                             [lead_created_at] =>
    186.                             [lead_created_by] =>
    187.                             [import_id] =>
    188.                             [lead_called_to] =>
    189.                             [phones] => Array
    190.                                 (
    191.                                     [0] => Array
    192.                                         (
    193.                                             [id] => 10582073
    194.                                             [phone] => +74959999999
    195.                                             [type] => work
    196.                                         )
    197.  
    198.                                 )
    199.  
    200.                             [contacts] => Array
    201.                                 (
    202.                                 )
    203.  
    204.                             [labels] => Array
    205.                                 (
    206.                                 )
    207.  
    208.                         )
    209.  
    210.                 )
    211.  
    212.             [totalCount] => 4
    213.         )
    214.  
    215.     [sandbox] => 1
    216. )
     
  7. Drunkenmunky

    Drunkenmunky Активный пользователь

    С нами с:
    12 авг 2020
    Сообщения:
    1.476
    Симпатии:
    281
    По ключам.
    Находите нужный элемент, и идете вверх по ветке, записывая найденные ключи справа налево.
    ...[phones][0][phone] и т.д.
    --- Добавлено ---
    Более удобный способ предоставляют некоторые браузеры.
    Открой ваш json например в Файрфоксе.
     
    pix3x нравится это.
  8. pix3x

    pix3x Новичок

    С нами с:
    26 фев 2021
    Сообщения:
    8
    Симпатии:
    0
    Спасибо, заработало:
    Код (Text):
    1. print_r($answerObject['data']['customers'][0]['phones'][0]['phone']);