Merge 3 rows that have json value - php

I have in a database table 3 rows that are json data and i want them merge they in a array, how can fix it?
Row 1 : ["11,22,13"]
Row 2 : ["48"]
Row 3 : ["53,67,70"]
I want in output as: array(11,22,13,48,53,67,70)
My tried as:
$result = $this->db->get_where('table',array('mainpage'=>$mp'));
$data = array();
foreach($result->result() as $row){
$dv = json_decode($row->sbouy);
$out = array();
foreach($dv as $idx => $val){
$out[] = $val;
}
echo '<pre>';
print_r($out); // This is not what I want output
}
In my code output is as(This is not what I want output):
Array
(
[0] => 11,22,13
)
Array
(
[0] => 48
)
Array
(
[0] => 53,67,70
)

Use this
$data = array();
$out = array();
foreach($result->result() as $row){
$dv = json_decode($row->sbouy);
foreach($dv as $idx => $val){
$out[] = $val;
}
}
echo '<pre>';
print_r($out); // This is what you want :)
You have to define $out outside foreach. Instead of loop you can even use array_merge

Related

PHP: How to iterate over array to retrieve incremental value (n, n+1, etc)

I have an array like this
$data = array(51729,49359,47548,8242,8124,8716,19610,18030,15698);
And a index number
$index = 3;
Is there a simple way to iterate over $data to get
$first = array(51729,8242,19610)
$second = array(49359,8124,18030)
$third = array(47548,8716,15698)
Is array_chunk() and then foreach the chunks the way to go?
Edit: Here is how I made it with array_chunk()
$data = array(51729,49359,47548,8242,8124,8716,19610,18030,15698);
$index = 3;
$chunks = array_chunk($data, $index);
$first = array();
$second = array();
$third = array();
foreach($chunks as $out) {
$first[] = $out[0];
$second[] = $out[1];
$third[] = $out[2];
}
Edit 2: All of this is part of transforming a unidimensional array to multidimensional, naming will eventually be dynamic, based on values in array $labels. Index number is also going to change ($index is provided in original array).
To do it dynamically, you can create a multidimensional array.
// Your provided values
$data = array(51729,49359,47548,8242,8124,8716,19610,18030,15698);
$index = 3;
// Initialize your result array
$result = array();
// Loop through the array
foreach ($data as $key => $value) {
// Assign every nth to the appropriate sub-array
$result[$key % $index][] = $value;
}
// Print for validation
print_r($result);
Provides:
Array
(
[0] => Array
(
[0] => 51729
[1] => 8242
[2] => 19610
)
[1] => Array
(
[0] => 49359
[1] => 8124
[2] => 18030
)
[2] => Array
(
[0] => 47548
[1] => 8716
[2] => 15698
)
)
If you are looking to do this non-dynamically. i.e. it will always be named this way, you're looking to continuously iterate in this manner with little changing, then the following code will be sufficient.
$i = 1;
foreach($data as $arr){
switch($i){
case 1:
array_push($first, $arr);
break;
case 2:
array_push($second, $arr);
break;
case 3:
array_push($third, $arr);
break;
}
$i++;
if($i == 4){
$i = 1;
}
}

PHP array values

I have an array:
Array
(
[0] => hello;string1
[1] => bye;string2
)
I want to get the array so I have the value of the string (after the ;) to be a value of the array key, so it becomes multi dimensional
$arr = array
(
"hello;string1",
"bye;string2"
);
$newArr = [];
foreach ($arr as $a) {
$e = explode(";", $a);
$newArr[$e[1]] = $e[0];
}
print_r($newArr);
You could do this using an explode
an explode explodes a variable on a delimitter, in your case ;
it puts the exploded values inside an array
<?php
$arr = ['test;hi', 'another;one'];
$newarr = [];
foreach ($arr as $key => $value) {
$value = explode(';', $value);
$newarr[$value[0]] = $value[1];
}
var_dump($newarr);
Output of the var_dump:
array(2) { ["test"]=> string(2) "hi" ["another"]=> string(3) "one" }
$array=Array
(
0 => "hello;string1",
1 => "bye;string2"
);
$result=array();
foreach($array as $data)
{
list($value,$key)=explode(";", $data);
$result[$key]=$value;
}
print_r($result);
Thanks for all your help, this actually what i wanted, sorry for being vague
foreach ($arr as $a) {
$e = explode(";", $a);
$newarr[] = array('hello' => $e[0], 'bye' => $e[1]);
}
You should use something like this!!
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}
Outout
Row number 0
Volvo
22
18
Row number 1
BMW
15
13
Row number 2
Saab
5
2
Row number 3
Land Rover
17
15
I fixed my answer to the new code above also you can find it here at https://www.w3schools.com/php/php_arrays_multi.asp

i want to array key and array value comma separated string

I have this array and I want to convert it into string.
I try to get string from php implode() function but could not get the desired result.
The output I want is arraykey-arrayvalue,arraykey-arrayvalue,arraykey-arrayvalue and so on as long as array limit end.
Array ( [1] => 1 [2] => 1 [3] => 1 )
$data = implode(",", $pData);
//it is creating string like
$data=1,1,1;
// but i want like below
$string=1-1,2-1,3-1;
You could just gather the key pair values inside an array then implode it:
foreach($array as $k => $v) { $data[] = "$k-$v"; }
echo implode(',', $data);
You can also use array_map function as
$arar = Array ( '1' => 1 ,'2' => 1, '3' => 1 );
$result = implode(',',array_map('out',array_keys($arar),$arar));
function out($a,$b){
return $a.'-'.$b;
}
echo $result;//1-1,2-1,3-1;
This can be done using the below code:
$temp = '';
$val = '';
$i=0;
foreach ($array as $key => $value)
{
$temp = $key.'-'.$val;
if($i == 0)
{
$val = $temp; // so that comma does not append before the string starts
$i = 1;
}
else
{
$val = $val.','.$temp;
}
}

Array Implode in php

I have an array of the following format:
$var = Array
(
[0] => Array
(
[name] => Harry
)
[1] => Array
(
[name] => Wayne
)
)
Array
(
[0] => Array
(
[name] => Wayne
)
I want to implode this array such that i get it in the format:
Harry,Wayne
Wayne
From What I have Tried I am getting it in format:
Harry,Wayne
Harry,Wayne,Wayne
What I have Tried (Not important as its wrong)
foreach($var as $a){
foreach($a as $b){
}$c[] = $b
}
$imp = implode(',',$c);
$var is fetched from database using fetch_array.
$this->db->select('name');
$this->db->where('id', $Id);
$this->db->from('info');
$row = $this->db->get();
$var = $row->result_array();
where $Id is array containing certain user ids.
foreach($var as $a)
{
unset($temp);
foreach($a as $b)
{
$temp[] = $b['name'];
}
$c[] = implode(",", $temp);
}
// output all the names
foreach ($c as $csvNames)
{
echo $csvNames;
}
Try this.
foreach($var as $a){
$m = '';
$delim = '';
foreach($a as $k){
$m .= $delim . $k['name'];
$delim = ',';
}
$c[] = $m;
}
foreach($c as $d){
echo $d;
}
Please ignore those hard-coded loops. There is a recursive function for it.
array_walk_recursive($var, create_function('$val, $key', 'array_push($obj, $val);'), &$output);
echo implode(",",$output);

Why are all indices in an array 0?

Why does the following code output 0 indices?
I want the indices to be: 0 1 2 3 4 .... How do I fix it?
Code:
foreach ($query->result() as $row){
$data = json_decode($row->residence,true);
foreach($data as $datum){
$newArray = array_chunk($datum['units'], 3);
foreach($newArray as $newA){
$output = array(implode(",",$newA));
echo print_r($output).'<br>'; //this is output
}
}
}
Output:
Array ( [0] => salam,11,11 ) 1 Array ( [0] => khobe,22,22 ) 1
Array ( [0] => salam,55,55 ) 1 Array ( [0] => khobe,66,66 ) 1
I want this output:
Array ( [0] => salam,11,11 ) 1 Array ( [1] => khobe,22,22 ) 1
Array ( [2] => salam,55,55 ) 1 Array ( [3] => khobe,66,66 ) 1
Update:
My JSON encoded in the database:
[{
"units": ["salam", "11", "11", "khobe", "22", "22"],
}, {
"units": ["salam", "55", "55", "khobe", "66", "66"],
}]
On this line $output = array(implode(",",$newA)); you are creating a new array for each set of results, so the offset with always start at 0.
Try this:
$output = array();
foreach ($query->result() as $row){
$data = json_decode($row->residence,true);
foreach($data as $unit) {
$output[] = implode(',', $unit['units']);
}
}
print_r($output);
I get the following output from a single row:
Array
(
[0] => salam,11,11,khobe,22,22
[1] => salam,55,55,khobe,66,66
)
In the third foreach, you need to add the $output to a new array, then print_r after the loop.
E.g.
foreach ($query->result() as $row){
$data = json_decode($row->residence,true);
foreach($data as $datum){
$newArray = array_chunk($datum['units'], 3);
$count = 0;
foreach($newArray as $newA){
$output = array($count =>implode(",",$newA));
$count++;
print_r($output).'<br>'; //this is output
}
}
}
Or similar.
You might need to move the $count = 0;
Try this instead
$output[] = array(implode(",",$newA));
You can do so by using a standard for loop like this instead of the foreach loop:
for ($i = 0; $i < count($newArray); $i++) {
// ...
}
You are creating all separate arrays every time you call the array function. To add items to the same array, use $output[] = $valueToAdd:
$output = array();
foreach ($query->result() as $row){
$data = json_decode($row->residence,true);
foreach($data as $datum){
$newArray = array_chunk($datum['units'], 3);
foreach($newArray as $newA){
$output[] = implode(",",$newA);
}
}
}
print_r($output).'<br>'; //this is output

Categories