How can I convert PHP array to accociative array [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to convert this array based on array value.
Before underscore value is convert to key and after '_" value is convert to value
Original array
Array (
[0] => 1_1
[1] => 1_2
[2] => 1_3
[3] => 1_4
[4] => 1_5
[5] => 1_6
[6] => 3_1
[7] => 3_2
[8] => 3_4
[9] => 3_5
[10] => 4_1
[11] => 4_2
[12] => 4_3
)
I want to convert it to :
Array(
[1]=> array(
[0]=>1,
[1]=>2,
[2]=>3,
[3]=>4,
[4]=>5,
[5]=>6
),
[3]=> array(
[0]=>1,
[1]=>2,
[2]=>4,
[3]=>5,
[4]=>5
),
[4]=> array(
[0]=>1,
[1]=>2,
[2]=>3
)
);

This should do the job :
$array = Array (
0 => "1_1",
1 => "1_2",
2 => "1_3",
3 => "1_4",
4 => "1_5",
5 => "1_6",
6 => "3_1",
7 => "3_2",
8 => "3_4",
9 => "3_5",
10 => "4_1",
11 => "4_2",
12 => "4_3"
);
// Create a new empty array
$result = array();
// Loop throught your first array
foreach ($array as $data) {
// Foreach data, you explode by '_'so you will get an array
// Eg. $new_data = array(0 => '1', 1 => '1') for $data = "1_1"
$new_data = explode('_', $data);
// Now you just have to add the data as you want
$result[$new_data[0]][] = $new_data[1];
}
var_dump($result);
The output is :
array (size=3)
1 =>
array (size=6)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
3 => string '4' (length=1)
4 => string '5' (length=1)
5 => string '6' (length=1)
3 =>
array (size=4)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '4' (length=1)
3 => string '5' (length=1)
4 =>
array (size=3)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)

This should work:
$resp = [];
foreach ($arr as $a) {
$values = explode('_', $a);
$resp[$values[0]][] = $values[1];
}
For the reverse:
$resp = [];
foreach ($resp as $key => $arr) {
foreach ($arr as $a) {
$resp[] = $key . '_' . $a;
}
}

Related

multidimensional array value insert into database using codeigniter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
array (size=11)
'reward_title' =>
array (size=2)
0 => string 'kishan' (length=6)
1 => string 'asd' (length=3)
'amount' =>
array (size=2)
0 => string '100' (length=3)
1 => string '200' (length=3)
'description' =>
array (size=2)
0 => string 'k' (length=1)
1 => string 'kk' (length=2)
'estimated_delivery' =>
array (size=2)
0 => string '02/02/2017' (length=10)
1 => string '02/03/2017' (length=10)
'shipping_details' =>
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
'ship_amount_country' =>
array (size=2)
0 => string '1' (length=1)
1 => string '' (length=0)
'ship_anywhere_world' =>
array (size=2)
0 => string '' (length=0)
1 => string '5' (length=1)
'limit_avail' =>
array (size=2)
0 => string 'on' (length=2)
1 => string 'on' (length=2)
'backer_limit' =>
array (size=2)
0 => string '2' (length=1)
1 => string '6' (length=1)
'avail_from' =>
array (size=2)
0 => string '3' (length=1)
1 => string '7' (length=1)
'avail_until' =>
array (size=2)
0 => string '4' (length=1)
1 => string '8' (length=1)
database table
screenshot
multidimensional array value insert into database using codeigniter
help me.
Try this way,
This will be your own function code,
$data = [];
foreach ($array as $k => $v) {
foreach ($v as $k1 => $v1) {
$data[$k1][] = [$k => $v1];
}
}
foreach ($data as $k => $v) {
$a = $this->array_2d_to_1d($v);
$result[] = $a;
}
$this->db->set($result);
$this->db->insert_batch('table', $result);
And make another function in that controller as
function array_2d_to_1d($input_array)
{
$output_array = array();
for ($i = 0; $i < count($input_array); $i++) {
for ($j = 0; $j < count($input_array[$i]); $j++) {
$output_array[key($input_array[$i])] = $input_array[$i][key($input_array[$i])];
}
}
return $output_array;
}
I am sure this will work.

how to select * from table where col1+col2=b using active record

How to run select * from table where col1+col2=b; using codeigniter's active record. Using mysql
I tried this but fail
$this->db->where("col1 + col2",$b)->get("table")->result_array();
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '915087' at line 4
SELECT * FROM table WHERE a + b 25
You can do that how you were thought. Just fixed out your mistake
follow the below code. Thanks. If it's useful please acknowledge it.
$b = 25;
$data = $this->db->where(array("(number1 + number2) = " => $b))->get("dummy_table")->result_array();
echo "<pre>";
print_r($data);
// output
Array
(
[0] => Array
(
[id] => 1
[number1] => 10
[number2] => 15
[number3] => 25
)
[1] => Array
(
[id] => 2
[number1] => 23
[number2] => 2
[number3] => 25
)
[2] => Array
(
[id] => 3
[number1] => 9
[number2] => 16
[number3] => 25
)
[3] => Array
(
[id] => 4
[number1] => 23
[number2] => 2
[number3] => 25
)
)
$this->db->select('(col1+col2) as col3');
$this->db->from('table');
$this->db->HAVING('col3 = ', $b);
$query = $this->db->get();
Try this.
Hope it will solved your problem. If you have any further query you can knock me feel free.
$this->load->model('dummy_table_model');
$b = 25;
$data = $this->db->get_where("dummy_table", array("(number1 + number2) = " => $b))->result_array();
// $this->db->last_query();
var_dump($data);
// Out put
array (size=4)
0 =>
array (size=4)
'id' => string '1' (length=1)
'number1' => string '10' (length=2)
'number2' => string '15' (length=2)
'number3' => string '25' (length=2)
1 =>
array (size=4)
'id' => string '2' (length=1)
'number1' => string '23' (length=2)
'number2' => string '2' (length=1)
'number3' => string '25' (length=2)
2 =>
array (size=4)
'id' => string '3' (length=1)
'number1' => string '9' (length=1)
'number2' => string '16' (length=2)
'number3' => string '25' (length=2)
3 =>
array (size=4)
'id' => string '4' (length=1)
'number1' => string '23' (length=2)
'number2' => string '2' (length=1)
'number3' => string '25' (length=2)

php array count same key after merge to separate comma

I using codeigniter 3.x version.
This is value passed to the ajax.
Array (
[0] => 10:4
[1] => 11:1
[2] => 12:2
[3] => 13:3
[4] => 14:4
[5] => 15:5
[6] => 16:4
[7] => 17:3
[8] => 18:2
[9] => 19:1
[10] => 1:4 // duplicate
[11] => 1:5 // duplicate
[12] => 20:2
[13] => 21:3
[14] => 22:4
[15] => 23:5
[16] => 24:4 // duplicate
[17] => 24:5 // duplicate
[18] => 25:2
[19] => 2:5
[20] => 3:4
[21] => 4:4
[22] => 5:4
[23] => 6:3
[24] => 7:2
[25] => 8:3
[26] => 9:2 // duplicate
[27] => 9:4 // duplicate
)
I want to insert into the database, Processing the array
First, sort to use natsort($data)
1:4
1:5
2:5
3:4
4:4
5:4
6:3
7:2
8:3
9:2
10:4
11:1
12:2
13:3
14:4
15:5
16:4
17:3
18:2
19:1
20:2
21:3
22:4
23:5
24:4
24:5
25:2
25:5
It was satisfactory.
but
1:4
1:5
24:4
24:5
25:2
25:5
is has duplicate value
My questions is
1:4
1:5
24:4
24:5
25:2
25:5
to
1:4,5
2:5
3:4
.
.
24:4,5
25:2,5
How should this be handled on php?
This might help -
$new= [];
foreach($ar as $v) {
$temp = explode(':', $v);
$new[$temp[0]][] = $temp[1];
}
foreach($new as $k => $v) {
$new[$k] = $k . ':' . implode(',', $v);
}
DEMO
$keys = array();
$list = array(
'1:4','1:5','2:1','3:4','4:1','4:2'
);
// Seach the keys
foreach($list as $item){
$key = substr($item,0,strpos($item, ':'));
$val = substr($item,strpos($item, ':')+1);
if(!isset($keys[$key])) $keys[$key] = array();
$keys[$key][] = $val;
}
//And now you can handle how do you want
$final = array();
foreach($keys as $key => $value){
$final[$key] = implode(',',$value);
}
var_dump($list);
var_dump($keys);
var_dump($final);
===================== OUTPUT =====================
$list:
array (size=6)
0 => string '1:4' (length=3)
1 => string '1:5' (length=3)
2 => string '2:1' (length=3)
3 => string '3:4' (length=3)
4 => string '4:1' (length=3)
5 => string '4:2' (length=3)
$keys:
array (size=4)
1 =>
array (size=2)
0 => string '4' (length=1)
1 => string '5' (length=1)
2 =>
array (size=1)
0 => string '1' (length=1)
3 =>
array (size=1)
0 => string '4' (length=1)
4 =>
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
$final:
array (size=4)
1 => string '4,5' (length=3)
2 => string '1' (length=1)
3 => string '4' (length=1)
4 => string '1,2' (length=3)
You can achieve it in two step ..
Get all first part and set values to all duplicates by making it key
Reverse engineering to achieve result
$a=array('1:4' ,'1:5' ,'24:4','24:5','25:2','25:5');//input here
$temp=array();
$result=array();
foreach($a as $v)
{
$part=explode(":",$v);
if(isset($temp[$part[0]]))
$temp[$part[0]].=",".$part[1];
else
$temp[$part[0]]=$part[1];
}
foreach($temp as $k=>$v)
{
$result[]=$k.":".$v;
}
print_r($result);
OUPTUT
Array ( [0] => 1:4,5 [1] => 24:4,5 [2] => 25:2,5 )

Sort php different format multidimensional array on key

I have an array like this
Array
(
[name] => Array
(
[0] => img/test240.jpg
[1] => img/cs1.jpg
[2] => img/cs2.jpg
[3] => img/cs3.jpg
)
[link] => Array
(
[0] => http://google.com
[1] => http://google.com
[2] => http://facebook.com
[3] => http://orkut.com
)
[order] => Array
(
[0] => 4
[1] => 1
[2] => 2
[3] => 3
)
)
I need to sort it by order WHICH IS KEY in Multidimensional array. Here is output.
Array
(
[name] => Array
(
[1] => img/cs1.jpg
[2] => img/cs2.jpg
[3] => img/cs3.jpg
[0] => img/test240.jpg
)
[link] => Array
(
[1] => http://google.com
[2] => http://facebook.com
[3] => http://orkut.com
[0] => http://google.com
)
[order] => Array
(
[1] => 1
[2] => 2
[3] => 3
[0] => 4
)
)
In this you can see when order is sorted name and link is also sorted according to the order. How can i do this with php.
You have to use array_map() in conjunction with sort().
If you want to preserve actual element order you have to use asort() instead sort().
Try this code:
$arr = array(
'name' => array(
0 => 'img/test240.jpg',
1 => 'img/cs1.jpg',
2 => 'img/cs2.jpg',
3 => 'img/cs3.jpg',
),
'link' => array(
0 => 'http://google.com',
1 => 'http://google.com',
2 => 'http://facebook.com',
3 => 'http://orkut.com',
),
'order' => array(
0 => 4,
1 => 1,
2 => 2,
3 => 3,
),
);
function mysort($a) {
asort($a);
return $a;
}
$arr = array_map('mysort', $arr);
print_r($arr);
Demo.
Try this, it uses array_multisort:
$array holds:
array (size=3)
'name' =>
array (size=4)
0 => string 'img/test240.jpg' (length=15)
1 => string 'img/cs1.jpg' (length=11)
2 => string 'img/cs2.jpg' (length=11)
3 => string 'img/cs3.jpg' (length=11)
'link' =>
array (size=4)
0 => string 'http://google.com' (length=17)
1 => string 'http://google.com' (length=17)
2 => string 'http://facebook.com' (length=19)
3 => string 'http://orkut.com' (length=16)
'order' =>
array (size=4)
0 => string '4' (length=1)
1 => string '1' (length=1)
2 => string '2' (length=1)
3 => string '3' (length=1)
Code:
$sort = array();
foreach($array as $k) {
foreach($k as $ind=>$val){
$sort['name'][$ind] = $array['name'][$ind];
$sort['link'][$ind] = $array['link'][$ind];
$sort['order'][$ind] = $array['order'][$ind];
}
}
array_multisort($sort['order'], SORT_ASC, $sort['link'], SORT_ASC, $sort['name'], SORT_ASC);
var_dump($sort);
Output:
array (size=3)
'name' =>
array (size=4)
0 => string 'img/cs1.jpg' (length=11)
1 => string 'img/cs2.jpg' (length=11)
2 => string 'img/cs3.jpg' (length=11)
3 => string 'img/test240.jpg' (length=15)
'link' =>
array (size=4)
0 => string 'http://google.com' (length=17)
1 => string 'http://facebook.com' (length=19)
2 => string 'http://orkut.com' (length=16)
3 => string 'http://google.com' (length=17)
'order' =>
array (size=4)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
3 => string '4' (length=1)
$this_arr = array(1,2,3,0);
function my_sort_2($arr, $arrangement)
{
$flag = false;
foreach($arr as $key => $val)
{
if(is_array($arr[$key]))
{
$arr[$key] = my_sort_2($arr[$key],$arrangement);
$flag = true;
}
}
if($flag == false && is_array($arr) && is_assoc($arr) === false)
{
$temp = array();
for($i = 0; $i < count($arrangement); $i++)
{
if(isset($arr[$arrangement[$i]]))
{
$temp[$arrangement[$i]] = $arr[$arrangement[$i]];
unset($arr[$arrangement[$i]]);
}
}
//$arr = array_merge($temp,$arr);
$arr = $temp;
}
return $arr;
}
Include this function below to run my own function. Also credit to #Matt Whittingham where i got this code from
function is_assoc($array)
{
$keys = array_keys($array);
return array_keys($keys) !== $keys;
}
Now let's do some sortin'... print_r(my_sort_2($arr,$this_arr)); assuming $arr contains Shan's array.
The output is EXACTLY what you desired.
It'll search for nested array (at least intended) and see if it's in a standard numeric ordered keys (in short, not custom order - yet; and not assoc) then sort it the way you want.
Note: I know my code isn't that probably good, optimized or bug free and that's my second attempt, misunderstanding your requirements first time (see the function name?).
Well after some research i found a simple solution like this
asort($data['order']);
$keys = array_keys($data['order']);
$data['name'] = array_replace(array_flip($keys), $data['name']);
$data['link'] = array_replace(array_flip($keys), $data['link']);
$data['order'] = array_replace(array_flip($keys), $data['order']);
Although i dont want to apply array_replace and array_flip on all the keys but this is done for the time being. I will surely trying to find how i can do it with single instruction.

Remove a value from an array and put that value into another array

i have two arrays and i need to extract the values of the 2nd array depending on the value of $arr[0]["num"]
$arr = array(
0 => array(
"id" => 24,
"num" => 2
),
1 => array(
"id" => 25,
"num" => 5
)
2 => array(
"id" => 26,
"num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[$i];
}
}
is it possible to remove the values of the 2nd array and transfer it into a new array?
what my loop does is just copying the values from the start after each loop. i want to remove the copied values from the 2nd array.
The output should be like this:
Array
(
[24] => Array
(
[0] => 1
[1] => 2
)
[25] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
[26] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
I'd suggest using array_shift
$arr = array(
array(
"id" => 24,
"num" => 2
),
array(
"id" => 25,
"num" => 5
),
array(
"id" => 26,
"num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[0]; // *1
array_shift($array);
}
}
echo '<pre>';
print_r($new);
*1 You have to change this line as well. Since array_shift removes the first array entry, each iteration should access array[0].
Output:
Array
(
[24] => Array
(
[0] => 1
[1] => 2
)
[25] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
[26] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
Try this
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[$i];
// unset previous values, in first iteration it will remove 0, 1
unset($array[$i]);
}
// reset the array keys, so for loop $i will start from 0
$array = array_values($array);
}
Output:
array (size=3)
24 =>
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
25 =>
array (size=5)
0 => string '3' (length=1)
1 => string '4' (length=1)
2 => string '5' (length=1)
3 => string '6' (length=1)
4 => string '7' (length=1)
26 =>
array (size=3)
0 => string '8' (length=1)
1 => string '9' (length=1)
2 => string '10' (length=2)

Categories