This question already has answers here:
PHP Append an array to a sub array
(2 answers)
Closed 6 months ago.
i've been trying to make this array
$data = array (
'country' => '+57',
'message' => 'test',
'messageFormat' => 0,
'addresseeList' =>
array (
0 =>
array (
'mobile' => '1111',
'correlationLabel' => 'corelation ejemplo',
),
)
);
into this
$data = array();
$data['country'] = '+57';
$data['message'] = 'test';
$data['messageFormat'] = 0;
$data['addresseeList'] = array(
$data['mobile'] = '1111',
$data['correlationLabel'] = 'corelation ejemplo'
);
But when i try to convert this array into a json object i'm getting this
string(154) "{"country":"+57","message":"test","messageFormat":0,"mobile":"1111","correlationLabel":"corelation ejemplo","addresseeList":["1111","corelation ejemplo"]}"
but i should get something like this
string(128) "{"country":"+57","message":"test","messageFormat":0,"addresseeList":[{"mobile":"1111","correlationLabel":"corelation ejemplo"}]}"
Thanks in advance
The way you are inserting the address info actually places the mobile and correlationLabel at the root of the $data array.
instead, you need to add create an entirely new array to place as the only element inside an array you create at $data['addresseeList'].
$data = array();
$data['country'] = '+57';
$data['message'] = 'test';
$data['messageFormat'] = 0;
$data['addresseeList'][] = array(
'mobile' => '1111',
'correlationLabel' => 'corelation ejemplo'
);
echo json_encode($data, JSON_PRETTY_PRINT).PHP_EOL;
Related
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I am selecting values from my database, when I dump the $results I get;
array (
0 =>
(object) array(
'FieldName' => 'certification_name',
'FieldValue' => 'White Belt',
),
1 =>
(object) array(
'FieldName' => 'certification_name',
'FieldValue' => 'Yellow Belt',
),
)
I want to display the following on my page;
Certification List: White Belt, Yellow Belt
I have created a loop but when I echo the result I get this;
Certification List: Array Array
My PHP code;
foreach ($results as $result) {
$name = $result->FieldName;
$value = $result->FieldValue;
$items[] = array(
'name' => $name,
'value' => $value
);
}
$items = implode("\n", $items);
echo 'Certification List: ' .$items;
What do I need to change in order to get this working?
You shouldn't push arrays into $items, just push the values.
foreach ($results as $result) {
$items[] = $result->FieldValue;
}
$item_list = implode(", ", $items);
echo "Certification List: $item_list";
You can also replace the loop with:
$items = array_column($results, 'FieldValue');
I want to create an array with 3 types of information: name, id, and work.
First I want to just initialize it, so that I can later fill it with data contained in variables.
I searched how to initialize a multidimensional array, and how to fill it, and that's what I came up with:
$other_matches_info_array = array(array());
$other_matches_name = "carmen";
$other_matches_id = 3;
$other_matches_work = "SON";
array_push($other_matches_info_array['name'], $other_matches_name);
array_push($other_matches_info_array['id'], $other_matches_id);
array_push($other_matches_info_array['work'], $other_matches_work);
This is what I get when I print_r the array:
Array
(
[0] => Array
(
)
[name] =>
)
What did I do wrong?
very short answer:
$other_matches_info_array = array();
// or $other_matches_info_array = []; - it's "common" to init arrays like this in php
$other_matches_name = "carmen";
$other_matches_id = 3;
$other_matches_work = "SON";
$other_matches_info_array[] = [
'id' => $other_matches_id,
'name' => $other_matches_name
];
// so, this means: new element of $other_matches_info_array = new array that is declared like this.
You can simply create it like so:
$arrayMultiDim = [
[
'id' => 3,
'name' => 'Carmen'
],
[
'id' => 4,
'name' => 'Roberto'
]
];
Then later to add to just say:
$arrayMultiDim[] = ['id' => 5, 'name' => 'Juan'];
Try code below:
$other_matches_info_array_main = [];
$other_matches_name = "carmen";
$other_matches_id = 3;
$other_matches_work = "SON";
$other_matches_info_array['name'] = $other_matches_name;
$other_matches_info_array['id'] = $other_matches_id;
$other_matches_info_array['work'] = $other_matches_work;
$other_matches_info_array_main[] = $other_matches_info_array;
Demo
I'm trying to write to a variable inside an object and I can't find how to do it.
Array
(
[0] => stdClass Object
(
[id] => 3
[rota_name] => Tea and coffee
[rota_owner_name] => 9
[rota_notes] =>
[rota_entry] => {"rota_entry0":{"person":"8","rota_assignment_date":"2018-04-01 20:17:48","rota_role":""},"rota_entry1":{"person":"7","rota_assignment_date":"2018-04-08 20:17:48","rota_role":""},"rota_entry2":{"person":"11","rota_assignment_date":"2018-04-15 20:17:48","rota_role":""},"rota_entry3":{"person":"7","rota_assignment_date":"2018-04-22 20:17:48","rota_role":""},"rota_entry4":{"person":"10","rota_assignment_date":"2018-04-29 20:17:48","rota_role":""},"rota_entry5":{"person":"3","rota_assignment_date":"2018-05-06 20:18:20","rota_role":""},"rota_entry6":{"person":"11","rota_assignment_date":"2018-05-13 20:18:23","rota_role":""}}
[rota_advance_email_days] =>
[rota_reminder_sent] =>
)
I want to change person 8 to person 9
So I think that I need to get the rota_entry (using foreach) and then use Json_decode to get an array and then something but my brain now hurts :( I and don't know how to reset it back up to put into the database again.
I can find lots that talks about simple JSON decode or simple array parsing but not something to help with this
This code assumes $obj = the first entry in your array you show.
So $obj = Array[0]
$json = json_decode($obj->rota_entry);
$json->rota_entry0->person = 9;
$obj->rota_entry = json_encode($json);
This code changes rota_entry0 person 8 to 9
// Your original array
$array = [
0 => (object) [
'id' => 3,
'rota_name' => 'Tea and coffee',
'rota_owner_name' => 9,
'rota_notes' => '',
'rota_entry' =>' {"rota_entry0":{"person":"8","rota_assignment_date":"2018-04-01 20:17:48","rota_role":""},"rota_entry1":{"person":"7","rota_assignment_date":"2018-04-08 20:17:48","rota_role":""},"rota_entry2":{"person":"11","rota_assignment_date":"2018-04-15 20:17:48","rota_role":""},"rota_entry3":{"person":"7","rota_assignment_date":"2018-04-22 20:17:48","rota_role":""},"rota_entry4":{"person":"10","rota_assignment_date":"2018-04-29 20:17:48","rota_role":""},"rota_entry5":{"person":"3","rota_assignment_date":"2018-05-06 20:18:20","rota_role":""},"rota_entry6":{"person":"11","rota_assignment_date":"2018-05-13 20:18:23","rota_role":""}}',
'rota_advance_email_days' => '',
'rota_reminder_sent' => '',
]
];
// Create an empty object to replace the rota_entry key in the array
$rotaEntry = (object) [];
// Iterate through the original rota_entry and replace "person"
foreach (json_decode($array[0]->rota_entry) as $key => $value) {
// You can set whatever logic you want here
// For example: if ($key == "rota_entry4") {$value->person = 4;}
// I'm hardcoding "9"
$value->person = 9;
$rotaEntry->$key = $value;
}
// Assign the newly created (and modified) rotaEntry back to the original array
$array[0]->rota_entry = $rotaEntry;
Try this:
$array = (array) $object;
foreach($array as &$value){
$json = json_encode($value['rota_entry']);
$json -> rota_entry0 -> person = 9;
$value['rota_entry'] = json_encode($json);
}
$array = (object) $array;
good luck.
This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I am facing some problem to fix this issue. My array same as below
Array ( [0] => Array ( [coupon] => dasd ) [1] => Array ( [coupon] => aaa ) )
From here I want to display only coupon value as comma separated. Same as below
dasd,aaa
To solve this I tried below code but that's not solving my issue.
<?php
$option_settings = array(
array( 'coupon'=>'dasd'
),
array( 'coupon'=>'aaa')
);
print_r($option_settings );
echo $output = implode(',', $option_settings);
?>
Sample https://eval.in/784206
Try this code
<?php
$option_settings = array(
array( 'coupon'=>'dasd'
),
array( 'coupon'=>'aaa')
);
$arr = array();
foreach($option_settings as $opt) {
$arr[] = $opt['coupon'];
}
echo $output = implode(',', $arr);
?>
Or You can use php function array_column()
$coupans_arr =array_column($option_settings, 'coupon');
echo $output = implode(",", $coupans_arr);
Live Demo
use simple array_column method
$option_settings = array(
array( 'coupon'=>'dasd'
),
array( 'coupon'=>'aaa')
);
print_r($option_settings );
$coupans_arr =array_column($option_settings, 'coupon');
echo $output = implode(",", $coupans_arr);
https://eval.in/784217
This is what you want,
print_r(implode(',', array_column($array, 'coupon')));
Please let me know if this works:
$option_settings = array(
array( 'coupon'=>'dasd'
),
array( 'coupon'=>'aaa')
);
$result = '';
foreach($option_settings as $item)
{
$result .= $item['coupon'] .', ';
}
$result = rtrim($result, ', ');
https://eval.in/784220
help me to convert the following array in to json.
I tried to convert the array.
Array
(
[0] => Array
(
[c_code] => 200001
[itemname] => 303 10CAP
[c_pack_code] => PK0075
[c_web_img_link] =>
)
[1] => Array
(
[c_code] => 200005
[itemname] => 3P 4TAB
[c_pack_code] =>
[c_web_img_link] =>
)
)
current result for the following code is
public function searchOrder($idx, $data) {
if (!empty($data)) {
$result = OrderbukModel::func_get_searchlist($idx,$data);
if (!empty($result)) {
$resultArray[] = $result;
print_r(json_encode($result));
} else {
$resultArray[$idx] = ["Mysql returns empty result !"];
print_r(json_encode($resultArray));
exit;
}
}
}
now i got the result is like
[{"c_code":"200001","itemname":"303 10CAP","c_pack_code":"PK0075","c_web_img_link":""},{"c_code":"200005","itemname":"3P 4TAB","c_pack_code":"","c_web_img_link":""}]
But I need the result as follows
[{"c_code":"2000001","c_code":"200005"},
{"itemname":"303 10CAP","itemname":"3P 4TAB"},
{"c_pack_code":"PK0075","c_pack_code":""},
{"c_web_img_link":"","c_web_img_link":""}]
Example of how you can you make the json from array. Collect the data in two different array and after loop marge them and store the result in another array after that encode them.
Note: Your desired JSON is not a valid format, you can't use same index
for two data.
Online Example: https://3v4l.org/kdPDI
$arr = array(
array(
'c_code' => '200001',
'itemname' => '303 10CAP',
'c_pack_code' => 'PK0075',
'c_web_img_link' => ''
),
array(
'c_code' => '200005',
'itemname' => '3P 4TAB',
'c_pack_code' => '',
'c_web_img_link' => ''
)
);
$res1 = array();
$res2 = array();
foreach($arr as $val){
$res1['c_code'][] = $val['c_code'];
$res1['itemname'][] = $val['itemname'];
$res2['c_pack_code'][] = $val['c_pack_code'];
$res2['c_web_img_link'][] = $val['c_web_img_link'];
}
$out = array(array_merge($res1, $res2));
echo json_encode($out);