In my output code is as:
Array (
[0] => 53,67,70
[1] => 48
[2] => 11,22,13
);
I want in output as: array(11,22,13,48,53,67,70)
$result = $this->db->get_where('table',array('mainpage'=>$mp'));
$data = array();
$out = array();
foreach($result->result() as $row){
$dv = json_decode($row->subpage);
$out = array_merge($dv, $out);
}
return $out;
In my database table rows are as: (json data)
Row 1 : ["11,22,13"]
Row 2 : ["48"]
Row 3 : ["53,67,70"]
How must fixed php code for output as array(11,22,13,48,53,67,70) ?
$result = $this->db->get_where('table',array('mainpage'=>$mp));
$data = array();
$out = array();
foreach($result->result() as $row){
$dv = json_decode($row->subpage);
$flat = array();
foreach ( $dv as $item ) {
$flat = array_merge( $flat, explode( ',', $item ) );
}
$out = array_merge( $out, $flat );
}
return $out;
$array = array (array (53, 67, 70), array (48), array (11, 22, 13));
$combined_array = call_user_func_array ('array_merge', $array);
Related
$arr1 = [1,2,3];
$arr2 = [1,2,3,4];
$arr3 = [1,2,3,4,5];
echo max( count($arr1), count($arr2), count($arr3) ); // returns 5
with max I do get the count, but don't know which array is larger. How do I get the larger array's reference ($arr3 in this case)?
You should use multidimensional array to store your all arrays then, loop through the multidimensional array and find your largest array,
$lgArraySize = 0; // used for comparing the size of array
$lgArray = array(); // used to store reference of largest array
foreach($arraylist as $array) {
if(count($array) > $largeArraySize) {
$lgArray = &$array;
$lgArraySize = count($array);
}
}
print_r($largeArray);
Here is the one liner code.(that you want)
function findMax( $row ){
return count($row);
}
$maxArrayPos = array_search(max( array_map("findMax", $multi )), array_map("findMax", $multi ));
print_r($multi[$maxArrayPos]);
You can make this as one liner.
It's better to use indexes for such arrays, so that traversal is easy.
You can get reference to the larger array as follows -
$arr;
$arr[0] = [6,2,1,12,32,11];
$arr[1] = [1,2,3,4];
$arr[2] = [1,2,3,4,4,4,1,1,1,1];
$count = -1;//will hold max count
$big;//larger array will be stored here
foreach ($arr as $key => $value) {
$curCount = count($arr[$key]);//curCount holds current array size
//checking for biggest array count
if($curCount>$count){
$count = $curCount;
$big = &$arr[$key];
}
}
var_dump($big);
Maybe the logic is too bulky though!
Is this what you need?
$arr1 = [1,2,3];
$arr2 = [1,2,3,4];
$arr3 = [1,2,3,4,5];
$countmax = [
"arr1" => $arr1,
"arr2" => $arr2,
"arr3" => $arr3,
];
$value = max($countmax);
print_r($value);
Oputput:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Try this :
function laregr(){
$numargs = func_num_args();
$large_array = array();
if($numargs){
$arg_list = func_get_args();
$max = 0;
foreach($arg_list as $arg){
if(is_array($arg)){
$big = count($arg);
if($big >= $max ){
$max = $big;
$large_array = $arg;
}
}
}
}
return $large_array;
}
$arr1 = [1,2,3,4,5,6];
$arr2 = [1,2,3,4,5];
$arr4 = [1,2,3,4, 5,6,7];
$max_array = laregr($arr1, $arr2, $arr4); // large($arr1, ...)
print_r($max_array);
You may like to store all arrays inside an array and would like to do a foreach I think.
function max_length($array) {
$max = 0;
foreach($array as $child) {
if(count($child) > $max) {
$max = count($child);
}
}
return $max;
}
check this why it is better approach .
I have a array
["item1","item2","item3"]
I want to get an array of ["1","2","3"]
how to get that in php
You need this
$arr = ["item1","item2","item3"];
for ($i = 0; $i < sizeof($arr); $i++) {
// replace "item" with ""
$arr[$i] = str_replace("item","",$arr[$i]);
}
<?php
$given_array = ["item1","item2","item3"];
$new_array = array();
foreach ($given_array as $arr) {
$new_array[] = intval(preg_replace('/[^0-9]+/', '', $arr), 10);
}
echo '<pre>';
print_r($new_array);
?>
1) Simply use
$res = str_replace('item', '', $array);
Output
$res
Array
(
[0] => 1
[1] => 2
[2] => 3
)
2) Using array_map()
$array = array_map(
function($str) {
return str_replace('item', '', $str);
},
$array
);
I have two array
$array1 = array
(
array('A',0),
array('B',0),
array('C',0),
array('D',0),
array('E',0),
array('F',0),
)
$array2 = array
(
array('A',5),
array('B',6),
array('C',10),
array('F',23),
)
$array2 will be changing sometimes A keys is there or its not there. It is applied for all keys.
I want to create a new array or replace the array values in $array1 to
$array1 = array
(
array('A',5),
array('B',6),
array('C',10),
array('D',0),
array('E',0),
array('F',23),
)
Try something like below
if(count($array1) > count($array2)){
$tempArr1 = $array1;
$tempArr2 = $array2;
}else{
$tempArr1 = $array2;
$tempArr2 = $array1;
}
$newArr = array();
foreach($tempArr1 as $values){
$a = $values[0]; $n = $values[1];
foreach($tempArr2 as $key=>$val){
if($val[0] == $a){
$n = ($val[1] > $n) ? $val[1] : $n;
unset($tempArr2[$key]);
}
}
$newArr[] = array($a, $n);
}
print_r($newArr);
$array1 = array (
array('A',0),
array('B',0),
array('C',0),
array('D',0),
array('E',0),
array('F',0),
);
$array2 = array (
array('A',5),
array('B',6),
array('C',10),
array('F',23),
);
foreach( $array2 as $itemKey2 => $itemVal2 ) {
$found = false;
foreach( $array1 as $itemKey1 => $itemVal1 ) {
if( $itemVal1[0] == $itemVal2[0] ) {
$found = true;
$array1[$itemKey1][1] = $itemVal2[1];
break;
}
}
if( !$found )
$array1[] = $item2;
}
echo var_export( $array1, true );
In retrospect, this scenario seems needlessly complicated. Unless something else truly requires this structure, if possible use something like:
$array1 = array (
'A' => 0,
'B' => 0,
'C' => 0,
'D' => 0,
'E' => 0,
'F' => 0
);
$array2 = array (
'A' => 5,
'B' => 6,
'C' => 10,
'F' => 23
);
foreach( $array2 as $key => $val ) {
$array1[$key] = $val;
}
I have two arrays (below). Is it possible to convert them into json string?
Array
(
[0] => size
[1] => color
)
Array
(
[0] => L
[1] => Black
)
Output structure should be:
[
{"name":"size","value":"L"},
{"name":"color","value":"Black"}
]
Thanks!
Sure:
$array1 = array('size', 'color');
$array2 = array('L', 'Black');
$jsonArray = array();
foreach (array_combine( $array1, $array2 ) as $name => $value) {
$jsonArray[] = array('name' => $name, 'value' => $value);
}
echo $json = json_encode($jsonArray);
This gives you
[{"name":"size","value":"L"},{"name":"color","value":"Black"}]
this here should work:
$json = json_encode( array_combine( $array1, $array2 ) );
Something like this should work just how you want:
<?php
$keys = array("size", "color");
$values = array("L", "Black");
$array = array();
foreach ($keys as $i => $key) {
$array[] = array(
"name" => $key,
"value" => $values[$i]
);
}
$json = json_encode($array);
var_dump($json);
//string(62) "[{"name":"size","value":"L"},{"name":"color","value":"Black"}]"
?>
$array1 = array('size', 'color');
$array2 = array('L', 'Black');
$result = array_combine($array1 , $array2);
$json = array();
foreach($result as $key => $val){
$json[] = array('name' => $key, 'value' => $value);
}
$json = json_encode($json);
I think you are looking for this:
$array1 = array('size', 'color');
$array2 = array('L', 'Black');
for($i=0;$i<sizeof($array1);$i++)
{
$array3[]=array($array1[$i]=>$array2[$i]);
}
echo json_encode($array3);
?>
Output:
[{"size":"L"},{"color":"Black"}]
I have one string like:-
$attributes = "id=1 username=puneet mobile=0987778987 u_id=232";
Now, I want to get it in following associative array format:-
$attributes{'id' => 1, 'username' => puneet, 'mobile' => 0987778987, 'u_id' => 232}
Note:- These all values are separated by space only. Any help will be appreciable.
Thanks in advance
$final_array = array();
$kvps = explode(' ', $attributes);
foreach( $kvps as $kvp ) {
list($k, $v) = explode('=', $kvp);
$final_array[$k] = $v;
}
I can suggest you to do it with a regular expression:
$str = "id=1 username=puneet mobile=0987778987 u_id=232";
$matches = array();
preg_match_all( '/(?P<key>\w+)\=(?P<val>[^\s]+)/', $str, $matches );
$res = array_combine( $matches['key'], $matches['val'] );
working example in phpfiddle
$temp1 = explode(" ", $attributes);
foreach($temp1 as $v){
$temp2 = explode("=", $v);
$attributes[$temp2[0]] = $temp2[1];
}
EXPLODE
this code will solve your problem.
<?php
$attributes = "id=1 username=puneet mobile=0987778987 u_id=232";
$a = explode ( ' ', $attributes) ;
$new_array = array();
foreach($a as $value)
{
//echo $value;
$pos = strrpos($value, "=");
$key = substr($value, 0, $pos);
$value = substr($value, $pos+1);
$new_array[$key] = $value;
}
print_r($new_array);
?>
out put of this code is
Array ( [id] => 1 [username] => puneet [mobile] => 0987778987 [u_id] => 232 )
I think you have to split this string two times
divide with space
divide with '='