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.
Related
i have this array structure, and i want to find and compare with other array that i have.
This is the array that i want search:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '60000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
'vlr' => string '40000' (length=5)
'pcost' => string '0' (length=1)
'pcomp' => string '0' (length=1)
'sede' =>
array (size=1)
9 => string '0' (length=1)
'ptc' =>
array (size=2)
12 => string '0' (length=1)
11 => string '0' (length=1)
's' => string '' (length=0)
An this is the array with search values:
Array
(
[0] => Blanco
[1] => 32GB
[2] => Plastico
)
I have the key and value, but i need find, in this example the main key is 1 in the long and main array, how can i get that?
PD: the number of search values are the numbers inside main arrays
Let's say that elements is the name of the array that you want to iterate over and find the index whose first three values match the search values. You can simply iterate over the elements, checking if the values match and if they do, then you can store the index in a variable called $wantedKey:
$target = array(
'0' => Blanco
'1' => 32GB
'2' => Plastico
);
$wantedKey = null;
foreach($elements as $key => $elem){
if($elem[0] == $target[0] && $elem[1] == $target[1] && $elem[2] == $target[2]){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
In case you have an arbitrary amount of values, you can use a foreach to iterate over them and check if they match:
foreach($elements as $key => $elem){
$sameValues = true;
foreach($target as $t_key => $t_value){
if($elem[$t_key] != $t_value){
$sameValues = false;
break;
}
}
if($sameValues){
$wantedKey = $key;
break;
}
}
echo $wantedKey;
I ‘ve got an array like this =>
this is a result from a google analytics request. I asked for the amout of visits for the last three months.
$statPerMonth
array (size=2)
'08' => // The month (August)
array (size=34)
0 =>
array (size=3)
0 => string '08' (length=2) // Month again
1 => string 'admin.testweb.fr' (length=19) // host
2 => string '1' (length=1) // amount of visits
1 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'audigie-espace-auto.reseau-fivestar.fr' (length=38)
2 => string '6' (length=1)
2 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'www.audigie-espace-auto.reseau-fivestar.fr' (length=31)
2 => string '9' (length=1)
3 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'carrosserie-abberis.reseau-fivestar.fr' (length=38)
2 => string '7' (length=1)
'07' =>
array (size=47)
0 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'www.anothersite.testweb.fr' (length=13)
2 => string '1' (length=1)
1 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'admin.testweb.fr' (length=16)
2 => string '2' (length=1)
2 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'admin.testweb.fr' (length=19)
2 => string '1' (length=1)
3 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'audigie-espace-auto.reseau-fivestar.fr' (length=38)
2 => string '20' (length=2)
4 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'www.admin.testweb.fr' (length=19)
2 => string '1' (length=1)
This array respresent the amount of visits for my websites but
you can see that the values [‘08’][‘1’] and [‘08’][‘2’] are identicals (only ‘www.’ differs)
I want to merge those cells and add their value (because it’s the same site !) in order to get the total amount of visits for a site with it’s two hostnames.
Consider $sites as an array of Site Object (websites).
the getHost() method will return the site host for exemple ‘my-host.fr’ without the ‘www’
consider $statsPerMonth array explained above
finally consider this algorithm
foreach ($statsPerMonth as $actualMonth => $stats) {
foreach($sites as $site) {
$siteHost = $site->getHost();
foreach ($stats as $row) {
if (strstr($row['1'], $siteHost)) {
if(isset($globalStats[$actualMonth][$siteHost])) {
$globalStats[$actualMonth][$siteHost] = $globalStats[$actualMonth][$siteHost] + $row['2'];
} else {
$globalStats[$actualMonth][$siteHost] = 0;
$globalStats[$actualMonth][$siteHost] = $globalStats[$actualMonth][$siteHost] + $row['2'];
}
}
if(!isset($globalStats[$actualMonth][$siteHost])) {
$globalStats[$actualMonth][$siteHost] = 0;
}
}
}
}
This algorithm return the $globalStats array in this form
array (size=3)
'08' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 1
'audigie-espace-auto.reseau-fivestar.fr' => int 15
'carrosserie-abberis-fivestar.fr' => int 16
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 0
'07' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 2
'audigie-espace-auto.reseau-fivestar.fr' => int 20
'carrosserie-abberis-fivestar.fr' => int 0
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 4
'06' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 0
'audigie-espace-auto.reseau-fivestar.fr' => int 29
'carrosserie-abberis-fivestar.fr' => int 0
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 4
This is exaclty what I want but I think we can improve this algorithm to make it more efficient (because the arrays are big). Have you any idea in order to make this algorithm better ?
Thank you.
Try to replace all "foreach" by "for", such as :
foreach ($array as $key => $value) {
}
by
$keys = array_keys($array);
$keyCount = count($keys);
for($i = 0; $i < $keyCount; $i++) {
$key = $keys[$i];
$value = $array[$key];
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have two arrays.
First, lets call it array1:
array (size=8)
0 =>
array (size=6)
'id' => string '2' (length=1)
'domacin' => string 'Man City' (length=8)
'gost' => string 'Liverpool' (length=9)
'tip' => string '1' (length=1)
'kvota' => string '1.8' (length=3)
'status' => string 'Aktivan' (length=7)
1 =>
array (size=6)
'id' => string '4' (length=1)
'domacin' => string 'Inter' (length=5)
'gost' => string 'Milan' (length=5)
'tip' => string '1' (length=1)
'kvota' => string '2.5' (length=3)
'status' => string 'Aktivan' (length=7)
And second, array2:
'id' => string '2' (length=1)
'domacin' => string 'Man City' (length=8)
'gost' => string 'Liverpool' (length=9)
'tip' => string '1' (length=1)
'kvota' => string '1.8' (length=3)
'status' => string 'Gubitan' (length=7)
I want to merge this two arrays but to use values from second where id from first is equal with id from second.
Expected result would be:
array (size=8)
0 =>
array (size=6)
'id' => string '2' (length=1)
'domacin' => string 'Man City' (length=8)
'gost' => string 'Liverpool' (length=9)
'tip' => string '1' (length=1)
'kvota' => string '1.8' (length=3)
'status' => string 'Gubitan' (length=7)
1 =>
array (size=6)
'id' => string '4' (length=1)
'domacin' => string 'Inter' (length=5)
'gost' => string 'Milan' (length=5)
'tip' => string '1' (length=1)
'kvota' => string '2.5' (length=3)
'status' => string 'Aktivan' (length=7)
Check the status where index is 0. It changed from 'Aktivan'(from array1) to 'Gubitan' like we had in array2.
How to do that?
foreach ($firstArr as &$item) {
if ($item['id'] == $secondArr['id']) {
$item = $secondArr;
}
}
You can do this easily using two foreach loops :
foreach( $array1 as $key1=>$element1 ){
foreach( $array2 as $element2 ){
if ($element1['id'] == $element2['id'])
{
$array1[$key1]['status'] = $element2['status'];
}
}
}
I have to parse 2 kind of table
one is this
http://leghe.fantagazzetta.com/f12-13/ ("Classifica Generale")
and the other two table are here
http://leghe.fantagazzetta.com/f12-13/formazioni?id=30339&g=4
how can i extract this data to an array?
for the first table i wrote this code but I really don't know what I'm doing
<?php
require('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('http://leghe.fantagazzetta.com/f12-13/classifica');
$tabClassifica = $html->find('table#classifica tr');
foreach($tabClassifica as $n) {
$team=$n->find('td',0)->outertext;
$arrayTeams[] = array('teamname' => $team);
}
?>
<pre>
<? print_r($arrayTeams); ?>
</pre>
I've to get this Array structure
[1] => Array
(
[TeamName] => A.C. Tua
[Pt.] => 9
[G] => 4
[V] => 3
[N] => 0
[P] => 1
[G+] => 8
[G-] => 5
[Somma Punti] => 293,50
)
[2] => Array
(
[TeamName] => Ehi Team
[Pt.] => 7
[G] => 4
[V] => 2
[N] => 1
[P] => 1
[G+] => 5
[G-] => 5
[Somma Punti] => 279,50
)
[3] => Array
(
[TeamName] => Brontolo
Could someone guide me?
You can use PHPQuery to achieve what you want.
Here is an example code:
include('phpQuery-onefile.php');
$content = file_get_contents('http://leghe.fantagazzetta.com/f12-13/');
$html = phpQuery::newDocumentHTML($content);
$table = $html->find('#classifica tbody');
$general_ranking = array();
$i=0;
foreach($table->children('tr') as $tr){
/**
* #var DOMElement $tr
*/
$getTd = $tr->getElementsByTagName('td');
foreach($getTd as $td){
/**
* #var DOMElement $td
*/
$general_ranking['tr_'.$i][] = trim($td->textContent);
}
++$i;
}
This should display something similar to:
array (size=6)
'tr_0' =>
array (size=7)
0 => string 'A.C. Tua' (length=8)
1 => string '9' (length=1)
2 => string '4' (length=1)
3 => string '3' (length=1)
4 => string '0' (length=1)
5 => string '1' (length=1)
6 => string '293,50' (length=6)
'tr_1' =>
array (size=7)
0 => string 'Ehi Team' (length=8)
1 => string '7' (length=1)
2 => string '4' (length=1)
3 => string '2' (length=1)
4 => string '1' (length=1)
5 => string '1' (length=1)
6 => string '279,50' (length=6)
'tr_2' =>
array (size=7)
0 => string 'Brontolo' (length=8)
1 => string '7' (length=1)
2 => string '4' (length=1)
3 => string '2' (length=1)
4 => string '1' (length=1)
5 => string '1' (length=1)
6 => string '274,50' (length=6)
'tr_3' =>
array (size=7)
0 => string 'milanelcuore' (length=12)
1 => string '6' (length=1)
2 => string '4' (length=1)
3 => string '2' (length=1)
4 => string '0' (length=1)
5 => string '2' (length=1)
6 => string '281,00' (length=6)
'tr_4' =>
array (size=7)
0 => string 'LONGOBARDA' (length=10)
1 => string '5' (length=1)
2 => string '4' (length=1)
3 => string '1' (length=1)
4 => string '2' (length=1)
5 => string '1' (length=1)
6 => string '254,50' (length=6)
'tr_5' =>
array (size=7)
0 => string 'i puffi' (length=7)
1 => string '0' (length=1)
2 => string '4' (length=1)
3 => string '0' (length=1)
4 => string '0' (length=1)
5 => string '4' (length=1)
6 => string '258,00' (length=6)
Edit:
After answering I got interested in simple_html_dom so I decided to try it out.
The coding style is a little bit easier than PHPQuery but it's not that stable I think.
Anyway, here is the code you need to get it working:
include('simple_html_dom/simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('http://leghe.fantagazzetta.com/f12-13/classifica');
$tabClassifica = $html->find('table#classifica tr');
foreach($tabClassifica as $n) {
/**
* #var simple_html_dom_node $n;
*/
$tds = $n->find('td');
// Don't allow empty records.
$team = trim(strip_tags($tds[0]->innertext));
if($team == "" || $team == " ") continue;
$arrayTeams[] = array(
'TeamName' => $team,
'Pt.' => trim(strip_tags($tds[1]->innertext)),
'G' => trim(strip_tags($tds[2]->innertext)),
'V' => trim(strip_tags($tds[3]->innertext)),
'N' => trim(strip_tags($tds[4]->innertext)),
'P' => trim(strip_tags($tds[5]->innertext)),
'G+' => trim(strip_tags($tds[6]->innertext)),
'G-' => trim(strip_tags($tds[7]->innertext)),
'Somma Punti' => trim(strip_tags($tds[8]->innertext)),
);
}
I have this array
array
0 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
2 =>
array
'prodid' => string '3' (length=1)
'qty' => int 1
'price' => string '380.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
'prodid' => string '8' (length=1)
'qty' => int 1
'price' => string '300.00' (length=6)
'sid' => string '24' (length=2)
How can i split it in to a multi dimentional array where the values 'sid' match like so.
array
0 =>
array
0 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
0 =>
array
'prodid' => string '7' (length=1)
'qty' => int 1
'price' => string '200.00' (length=6)
'sid' => string '15' (length=2)
You pick the value you want to group with as key for a new array and push the member to it
$new = array();
foreach($array as $member)
{
$key = $member['group-value'];
$new[$key][] = $member;
}
You then extract the bare values from the new array by using array_values:
$new = array_values($new);
And that's it. If you want to spare the last part, you can also create the mapping of keys and it's numerical index on your own:
$new = array();
$keys = array();
foreach($array as $member)
{
$key = $member['group-value'];
isset($keys[$key]) || $keys[$key] = count($keys);
$new[$keys[$key]][] = $member;
}
For PHP5.3+
$result = array_reduce (
$array,
function ($item, $result) {
if (!isset($result[$item['sid']])) $result[$item['sid']] = array();
$result[$item['sid']][] = $item;
return $result;
},
array()
)
// $ouput array will be indexed by the 'sid' value
$output = array();
// Iterate over the main array and create a new subarray if
// it doesn't already exist, or add to it if it does.
foreach($input as $subarr) {
if (!isset($output[$subarr['sid']]) {
// New array indexed by sid
$output[$subarr['sid']] = array();
}
// Append the whole array
$output[$subarr['sid']][] = $subarr;
}
Note: this produces slightly different output than you described, in that it uses the sid as array keys rather than just indexing them from 0:
array
// Note key difference...
// sid == 13
13 =>
array
0 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
1 =>
array
'prodid' => string '2' (length=1)
'qty' => int 2
'price' => string '100.00' (length=6)
'sid' => string '13' (length=2)
// sid == 15
15 =>
array
0 =>
array
'prodid' => string '7' (length=1)
'qty' => int 1
'price' => string '200.00' (length=6)
'sid' => string '15' (length=2)