I want get all rows of ref table : ciudad
public function actionGetCiudades(){
$model = ciudad::model()->findAll();
$lCiudades = array();
$i=0;
$dataReader = Yii::app()->db->createCommand( 'SELECT * FROM ciudad' )->query();
foreach( $dataReader as $row ) {
$ciudad = new ciudad;
echo $row->ciudadid.":".$row->estadoid.":".$row->ciudaddsc.":".$row->activo;
echo "<br/";
unset( $ciudad );
}
Unfortunately, output is :
:::
But I have 700+ rows in my table.
What's wrong???
UPDATE :
var_dump($dataReader) -->
array(4) { ["ciudadid"]=> string(1) "1" ["estadoid"]=> string(1) "1" ["ciudaddsc"]=> string(15) "VALLE DE MEXICO" ["activo"]=> string(1) "1" } [1]=> array(4) { ["ciudadid"]=> string(1) "2" ["estadoid"]=> string(1) "1" ["ciudaddsc"]=> string(6) "TOLUCA" ["activo"]=> string(1) "1" } [2]=> array(4) { ["ciudadid"]=> string(1) "3" ["estadoid"]=> string(1) "1" ["ciudaddsc"]=> string(11) "ATLACOMULCO" ["activo"]=> string(1) "1" } [3]=> array(4) { ["ciudadid"]=> string(1) "4" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(14) "AGUASCALIENTES" ["activo"]=> string(1) "1" } [4]=> array(4) { ["ciudadid"]=> string(1) "5" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(18) "SAN JOSE DE GRACIA" ["activo"]=> string(1) "1" } [5]=> array(4) { ["ciudadid"]=> string(1) "6" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(19) "PABELLON DE ARTEAGA" ["activo"]=> string(1) "1" } [6]=> array(4) { ["ciudadid"]=> string(1) "7" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(8) "CALVILLO" ["activo"]=> string(1) "1" } [7]=> array(4) { ["ciudadid"]=> string(1) "8" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(19) "ENCARNACION DE DIAZ" ["activo"]=> string(1) "1" } [8]=> array(4) { ["ciudadid"]=> string(1) "9" ["estadoid"]=> string(1) "2" ["ciudaddsc"]=> string(15) "RINCON DE ROMOS" ["activo"]=> string(1) "1" } [9]=> array(4) { ["ciudadid"]=> string(2) "10" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(8) "ENSENADA" ["activo"]=> string(1) "1" } [10]=> array(4) { ["ciudadid"]=> string(2) "11" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(7) "TIJUANA" ["activo"]=> string(1) "1" } [11]=> array(4) { ["ciudadid"]=> string(2) "12" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(8) "MEXICALI" ["activo"]=> string(1) "1" } [12]=> array(4) { ["ciudadid"]=> string(2) "13" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(8) "ROSARITO" ["activo"]=> string(1) "1" } [13]=> array(4) { ["ciudadid"]=> string(2) "14" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(6) "TECATE" ["activo"]=> string(1) "1" } [14]=> array(4) { ["ciudadid"]=> string(2) "15" ["estadoid"]=> string(1) "3" ["ciudaddsc"]=> string(7) "LA MESA" ["activo"]=> string(1) "1" } [15]=> array(4) { ["ciudadid"]=> string(2) "16" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(6) "LA PAZ" ["activo"]=> string(1) "1" } [16]=> array(4) { ["ciudadid"]=> string(2) "17" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(9) "LOS CABOS" ["activo"]=> string(1) "1" } [17]=> array(4) { ["ciudadid"]=> string(2) "18" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(7) "COMONDU" ["activo"]=> string(1) "1" } [18]=> array(4) { ["ciudadid"]=> string(2) "19" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(14) "CABOS AN LUCAS" ["activo"]=> string(1) "1" } [19]=> array(4) { ["ciudadid"]=> string(2) "20" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(17) "SAN JOSE DEL CABO" ["activo"]=> string(1) "1" } [20]=> array(4) { ["ciudadid"]=> string(2) "21" ["estadoid"]=> string(1) "4" ["ciudaddsc"]=> string(19) "CIUDAD CONSTITUCION" ["activo"]=> string(1) "1" } [21]=> array(4) { ["ciudadid"]=> string(2) "22" ["estadoid"]=> string(1) "5" ["ciudaddsc"]=> string(8) "CAMPECHE" ["activo"]=> string(1) "1" } [22]=> array(4) { ["ciudadid"]=> string(2) "23" ["estadoid"]=> string(1) "5" ["ciudaddsc"]=> string(17) "CIUDAD DEL CARMEN" ["activo"]=> string(1) "1" } [23]=> array(4) { ["ciudadid"]=> string(2) "24" ["estadoid"]=> string(1) "5" ["ciudaddsc"]=> string(9) "ESCARCEGA" ["activo"]=> string(1) "1" } [24]=> array(4) { ["ciudadid"]=> string(2) "25" ["estadoid"]=> string(1) "5" ["ciudaddsc"]=> string(7) "CALAKUL" ["activo"]=> string(1) "1" } [25]=> array(4) { ["ciudadid"]=> string(2) "26" ["estadoid"]=> string(1) "5" ["ciudaddsc"]=> string(7) "CALKINI" ["activo"]=> string(1) "1" }
Use queryAll() instead of query(). query() adds limit 1 to the sql query and gets only first row matching criteria.
As the Question title is generic let me post a generic answer to the question.
i am using Yii 2.
to get All rows from a table without applying any where class here is how you will get.
$dbusers = New Users;
$users = $dbusers->find()->all();
Your model should look like this.
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
class Users extends ActiveRecord
{
public function rules()
{
return [
[['name', 'id','email','type'], 'required'],
['id', 'numeric'],
];
}
}
Final Code :
public function actionGetCiudades(){
$lCiudades = array();
$dataReader = Yii::app()->db->createCommand( 'SELECT * FROM ciudad' )->queryAll();
foreach( $dataReader as $row ) {
$ciudad->ciudadid = $row['ciudadid'];
$ciudad->estadoid = $row['estadoid'];
$ciudad->ciudaddsc = $row['ciudaddsc'];
$ciudad->activo = $row['activo'];
array_push($lCiudades, $ciudad);
unset( $ciudad );
}
$json = json_encode($lCiudades);
echo $json;
}
Related
guys lets play in multidimensional array little bit :p
lets said i have $filter which it contain multidimensional array :
array(2) {
["time"]=>
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
["people"]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "4"
[4]=>
string(1) "5"
}
}
from the array it should be return :
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "1"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "2"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "1"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "2"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "3"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "4"
["promo_id"]=>
string(1) "1"
}
array(3) {
["time"]=>
string(1) "3"
["people"]=>
string(1) "5"
["promo_id"]=>
string(1) "1"
}
actually i could get that result with this following code :
foreach ($filter['time'] as $row){
foreach ($filter['people'] as $item){
$new_array['time'] = $row;
$new_array['people'] = $item;
$new_array['promo_id'] = $promo_id;
vd($new_array,"new");
}
}
but i cant use that following code, because it using $filter['time'] and $filter['people'], how about if the time or people is a random string?
so can you show me guys, to looping it from $filter?
as like this :
foreach ($filter as $key => $row){
}
thank you (:
function combination($arr,$res,$completed_key){
$array_key = array_keys($arr);
$last_array_key = array_pop($array_key);
$start = false;
$temp = $res;
$res=array();
$i=0;
foreach($arr as $key=>$row){
if(!in_array($key,$completed_key)&& $start==false){
array_push($completed_key,$key);
$start=true;
}
if($start){
if(count($temp)==0){
foreach($row as $key2 => $row2){
$res[][$key]=$row2;
}
}else{
foreach($temp as $temp_key=>$temp_row){
foreach($row as $key2 => $row2){
$res[$i]=$temp_row;
$res[$i][$key]=$row2;
$i++;
}
}
}
}
if($start){
break;
}
}
if(!in_array($last_array_key,$completed_key)){
return $this->combination($arr,$res,$completed_key,$debugger);
}else{
return $res;
}
}
This function will return you want.
$filter["time"]=array(1,2,3);
$filter["people"]=array(1,2);
$filter["size"]=array("a","b");
$res=$this->combination($filter,array(),array());
I'll explain later.
guys i have 4 table uhd_voucher_menu, uhd_menu, uhd_voucher_variant_menu, uhd_variant_menu
which uhd_voucher_menu and uhd_menu is connected by menu_id, than uhd_voucher_variant_menu and uhd_voucher_menu is connected by voucher_menu_id and the last, uhd_variant_menu and voucher_variant_menu is connected by variant_menu_id
this is my code how i get the data :
$this->db->select("uhd_voucher_menu.voucher_id,uhd_menu.menu_name,uhd_voucher_menu.qty,uhd_variant_menu.variant_name,uhd_voucher_menu.voucher_menu_id");
$this->db->from("uhd_voucher_menu");
$this->db->join("uhd_menu","uhd_menu.menu_id = uhd_voucher_menu.menu_id");
$this->db->join("uhd_voucher_variant_menu","uhd_voucher_variant_menu.voucher_menu_id = uhd_voucher_menu.voucher_menu_id","left");
$this->db->join("uhd_variant_menu","uhd_variant_menu.variant_menu_id = uhd_voucher_variant_menu.variant_menu_id","left");
$this->db->where("voucher_id",$data["voucher"]["voucher_id"]);
$this->db->where("uhd_menu.restaurant_id",$restaurant_id);
$res = $this->db->get()->result_array();
from that active recored will be return to this data :
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(5) "Spicy"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "3"
}
[2]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "1"
["variant_name"]=>
string(4) "Sour"
["voucher_menu_id"]=>
string(1) "4"
}
[3]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "1"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "4"
}
[4]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(14) "Red Crown Crab"
["qty"]=>
string(1) "2"
["variant_name"]=>
NULL
["voucher_menu_id"]=>
string(1) "1"
}
[5]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(8) "Omellete"
["qty"]=>
string(1) "3"
["variant_name"]=>
NULL
["voucher_menu_id"]=>
string(1) "2"
}
as you see the golden_salmon with same voucher_menu_id is return twice
i want if the voucher_menu_id is same, so from this data :
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(5) "Spicy"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
string(6) "Medium"
["voucher_menu_id"]=>
string(1) "3"
}
i want that data return to become this :
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_name"]=>
string(13) "Golden Salmon"
["qty"]=>
string(1) "2"
["variant_name"]=>
array(2){
[1]=>
string(5) "Spicy"
[2]=>
string(6) "Medium"
}
["voucher_menu_id"]=>
string(1) "3"
}
same as the other golden_salmon with same voucher_menu_id 4
i want the variant_name is become array so the if data with same voucher_menu_id will not call twice
guys can you help me how to execute the data as i want?
thank you (:
You have to use group by, I slightly modified you query:
$this->db->select("uhd_voucher_menu.voucher_id,uhd_menu.menu_name,uhd_voucher_menu.qty,GROUP_CONCAT(uhd_variant_menu.variant_name SEPARATOR ',') AS variants,uhd_voucher_menu.voucher_menu_id");
$this->db->from("uhd_voucher_menu");
$this->db->join("uhd_menu","uhd_menu.menu_id = uhd_voucher_menu.menu_id");
$this->db->join("uhd_voucher_variant_menu","uhd_voucher_variant_menu.voucher_menu_id = uhd_voucher_menu.voucher_menu_id","left");
$this->db->join("uhd_variant_menu","uhd_variant_menu.variant_menu_id = uhd_voucher_variant_menu.variant_menu_id","left");
$this->db->where("voucher_id",$data["voucher"]["voucher_id"]);
$this->db->where("uhd_menu.restaurant_id",$restaurant_id);
$this->db->group_by("uhd_voucher_menu.voucher_id");
$res = $this->db->get()->result_array();
we can convert the comma seperated values to array after query output.
Try and post the result...
I have a multidimensional array $voucher_data with this following array :
array(6) {
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
string(1) "2"
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
string(1) "6"
["voucher_menu_id"]=>
string(1) "3"
}
[2]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "1"
["variant_menu_id"]=>
string(1) "1"
["voucher_menu_id"]=>
string(1) "4"
}
[3]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "1"
["variant_menu_id"]=>
string(1) "6"
["voucher_menu_id"]=>
string(1) "4"
}
[4]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "519"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "1"
}
[5]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "525"
["qty"]=>
string(1) "3"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "2"
}
}
In my array [0] and [1] the voucher_menu_id is 3. They have same voucher_menu_id and also [2] and [3] is 4
So i want to combine the variant_menu_id become 1 array become like this :
array(4) {
[0]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
array(2){
string(1) "2"
string(1) "6"
}
["voucher_menu_id"]=>
string(1) "3"
}
[1]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "521"
["qty"]=>
string(1) "1"
["variant_menu_id"]=>
array(2){
string(1) "1"
string(1) "4"
}
["voucher_menu_id"]=>
string(1) "4"
}
}
[2]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "519"
["qty"]=>
string(1) "2"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "1"
}
[3]=>
array(5) {
["voucher_id"]=>
string(1) "1"
["menu_id"]=>
string(3) "525"
["qty"]=>
string(1) "3"
["variant_menu_id"]=>
NULL
["voucher_menu_id"]=>
string(1) "2"
}
}
Guys can you help me how to combine it?
Thank you (:
Just like the comment above, just use your voucher_menu_id as your key into assigning the new container. Use that so that it'll be group when you loop:
$new_array = array();
foreach ($voucher_data as $v) {
if(empty($new_array[$v['voucher_menu_id']])) { // if empty initialize
$new_array[$v['voucher_menu_id']] = $v; // base array group
$new_array[$v['voucher_menu_id']]['variant_menu_id'] = array(); // intialize container
}
// push id
$new_array[$v['voucher_menu_id']]['variant_menu_id'][] = $v['variant_menu_id'];
}
$new_array = array_values($new_array); // array reindex
Untested, but something like this might work:
$output = array();
foreach ($array as $entry) {
// make the variant_menu_id an array
$entry['variant_menu_id'] = (array) $entry['variant_menu_id'];
$key = $entry['voucher_menu_id'];
if (!array_key_exists($key, $output)) {
// create a new entry in the output array using the entire $entry
$output[$key] = $entry;
continue;
}
// otherwise, add your variant menu ID to the existing values
$output[$key]['variant_menu_id'] = array_merge($output[$key]['variant_menu_id'], $entry['variant_menu_id']);
}
This will essentially create a new array by grouping entries by their voucher_menu_id, and adding variant_menu_id values into an array in the $output.
Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.
Hi I have a array with lets say 3 arrays with another few arrays inside those like this:
array(3) {
[0]=>
array(2) {
["disease_id"]=>
array(13) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(2) "15"
[6]=>
string(1) "4"
[7]=>
string(1) "3"
[8]=>
string(2) "18"
[9]=>
string(1) "9"
[10]=>
string(1) "2"
[11]=>
string(2) "20"
[12]=>
string(1) "1"
}
["params"]=>
object(stdClass)#160 (39) {
["disease_cpe_5"]=>
string(1) "1"
["disease_mbr_5"]=>
string(4) "1234"
["disease_mtr_5"]=>
string(2) "12"
["disease_cpe_14"]=>
string(1) "1"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_17"]=>
string(1) "4"
["disease_mbr_17"]=>
string(1) "1"
["disease_mtr_17"]=>
string(1) "1"
["disease_cpe_16"]=>
string(1) "4"
["disease_mbr_16"]=>
string(2) "21"
["disease_mtr_16"]=>
string(3) "122"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "1"
["disease_mbr_4"]=>
string(1) "1"
["disease_mtr_4"]=>
string(1) "9"
["disease_cpe_3"]=>
string(1) "7"
["disease_mbr_3"]=>
string(1) "8"
["disease_mtr_3"]=>
string(1) "9"
["disease_cpe_18"]=>
string(1) "1"
["disease_mbr_18"]=>
string(1) "1"
["disease_mtr_18"]=>
string(1) "1"
["disease_cpe_9"]=>
string(1) "3"
["disease_mbr_9"]=>
string(1) "3"
["disease_mtr_9"]=>
string(1) "3"
["disease_cpe_2"]=>
string(1) "3"
["disease_mbr_2"]=>
string(1) "3"
["disease_mtr_2"]=>
string(1) "3"
["disease_cpe_20"]=>
string(2) "10"
["disease_mbr_20"]=>
string(2) "11"
["disease_mtr_20"]=>
string(2) "12"
["disease_cpe_1"]=>
string(1) "1"
["disease_mbr_1"]=>
string(1) "3"
["disease_mtr_1"]=>
string(1) "3"
}
}
[1]=>
array(3) {
["disease_id"]=>
array(8) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(1) "8"
[6]=>
string(2) "15"
[7]=>
string(1) "4"
}
["risk_id"]=>
array(1) {
[0]=>
string(1) "4"
}
["params"]=>
object(stdClass)#235 (27) {
["disease_cpe_5"]=>
string(1) "2"
["disease_mbr_5"]=>
string(1) "1"
["disease_mtr_5"]=>
string(1) "1"
["disease_cpe_14"]=>
string(1) "2"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_17"]=>
string(1) "1"
["disease_mbr_17"]=>
string(1) "1"
["disease_mtr_17"]=>
string(1) "1"
["disease_cpe_16"]=>
string(1) "1"
["disease_mbr_16"]=>
string(1) "5"
["disease_mtr_16"]=>
string(1) "6"
["disease_cpe_8"]=>
string(2) "11"
["disease_mbr_8"]=>
string(1) "1"
["disease_mtr_8"]=>
string(1) "1"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "7"
["disease_mbr_4"]=>
string(1) "8"
["disease_mtr_4"]=>
string(1) "9"
["risk_cpe_4"]=>
string(1) "1"
["risk_mbr_4"]=>
string(1) "2"
["risk_mtr_4"]=>
string(1) "3"
}
}
[2]=>
array(3) {
["disease_id"]=>
array(6) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(1) "8"
[4]=>
string(2) "15"
[5]=>
string(1) "4"
}
["risk_id"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["params"]=>
object(stdClass)#184 (24) {
["disease_cpe_5"]=>
string(1) "2"
["disease_mbr_5"]=>
string(1) "1"
["disease_mtr_5"]=>
string(1) "1"
["disease_cpe_14"]=>
string(1) "2"
["disease_mbr_14"]=>
string(1) "2"
["disease_mtr_14"]=>
string(1) "2"
["disease_cpe_12"]=>
string(2) "12"
["disease_mbr_12"]=>
string(2) "12"
["disease_mtr_12"]=>
string(2) "12"
["disease_cpe_8"]=>
string(2) "11"
["disease_mbr_8"]=>
string(1) "1"
["disease_mtr_8"]=>
string(1) "1"
["disease_cpe_15"]=>
string(3) "132"
["disease_mbr_15"]=>
string(3) "132"
["disease_mtr_15"]=>
string(1) "1"
["disease_cpe_4"]=>
string(1) "7"
["disease_mbr_4"]=>
string(1) "8"
["disease_mtr_4"]=>
string(1) "9"
["risk_cpe_4"]=>
string(1) "1"
["risk_mbr_4"]=>
string(1) "2"
["risk_mtr_4"]=>
string(1) "3"
["risk_cpe_3"]=>
string(1) "5"
["risk_mbr_3"]=>
string(1) "5"
["risk_mtr_3"]=>
string(1) "5"
}
}
}
Now I need to merge these arrays in to one and where the values match up in the ["disease_id"] and ["risk_id"] sub array I should delete the duplicate, yet when the keys match up in the ["params"] object I need to keep both values and yet dump the duplicated key in the object, so that this is kind of the result:
array(1) {
[0]=>
array(2) {
["disease_id"]=>
array(13) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(2) "17"
[4]=>
string(2) "16"
[5]=>
string(2) "15"
[6]=>
string(1) "4"
[7]=>
string(1) "3"
[8]=>
string(2) "18"
[9]=>
string(1) "9"
[10]=>
string(1) "2"
[11]=>
string(2) "20"
[12]=>
string(1) "1"
[13]=>
string(1) "8"
}
["risk_id"]=>
array(1) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["params"]=>
object(stdClass)#160 (39) {
["disease_cpe_5"]=>
string(7) "1, 1, 2"
["disease_mbr_5"]=>
string(10) "1234, 1, 1"
["disease_mtr_5"]=>
string(8) "12, 1, 1"
["disease_cpe_14"]=>
string(7) "1, 2, 2"
["disease_mbr_14"]=>
string(7) "2, 2, 2"
["disease_mtr_14"]=>
string(7) "2, 2, 2"
["disease_cpe_12"]=>
string(10) "12, 12, 12"
["disease_mbr_12"]=>
string(10) "12, 12, 12"
["disease_mtr_12"]=>
string(10) "12, 12, 12"
["disease_cpe_17"]=>
string(4) "4, 1"
["disease_mbr_17"]=>
string(4) "1, 1"
["disease_mtr_17"]=>
string(4) "1, 1"
["disease_cpe_16"]=>
string(4) "4, 1"
["disease_mbr_16"]=>
string(5) "21, 5"
["disease_mtr_16"]=>
string(3) "122,6"
["disease_cpe_8"]=>
string(6) "11, 11"
["disease_mbr_8"]=>
string(1) "1, 1"
["disease_mtr_8"]=>
string(1) "1, 1"
["disease_cpe_15"]=>
string(13) "132, 132, 132"
["disease_mbr_15"]=>
string(13) "132, 132, 132"
["disease_mtr_15"]=>
string(7) "1, 1, 1"
["disease_cpe_4"]=>
string(7) "1, 7, 7"
["disease_mbr_4"]=>
string(7) "1, 8, 8"
["disease_mtr_4"]=>
string(7) "9, 9, 9"
["disease_cpe_3"]=>
string(1) "7"
["disease_mbr_3"]=>
string(1) "8"
["disease_mtr_3"]=>
string(1) "9"
["disease_cpe_18"]=>
string(1) "1"
["disease_mbr_18"]=>
string(1) "1"
["disease_mtr_18"]=>
string(1) "1"
["disease_cpe_9"]=>
string(1) "3"
["disease_mbr_9"]=>
string(1) "3"
["disease_mtr_9"]=>
string(1) "3"
["disease_cpe_2"]=>
string(1) "3"
["disease_mbr_2"]=>
string(1) "3"
["disease_mtr_2"]=>
string(1) "3"
["disease_cpe_20"]=>
string(2) "10"
["disease_mbr_20"]=>
string(2) "11"
["disease_mtr_20"]=>
string(2) "12"
["disease_cpe_1"]=>
string(1) "1"
["disease_mbr_1"]=>
string(1) "3"
["disease_mtr_1"]=>
string(1) "3"
["risk_cpe_4"]=>
string(1) "1, 1"
["risk_mbr_4"]=>
string(1) "2, 2"
["risk_mtr_4"]=>
string(1) "3, 3"
["risk_cpe_3"]=>
string(1) "5"
["risk_mbr_3"]=>
string(1) "5"
["risk_mtr_3"]=>
string(1) "5"
}
}
}
I have tried many thing but have not found an answer that effectively solves this issue. Here is one of my attempts:
$newArray = array();
if( !is_object($newArray["parpams"]) ){
$newArray["parpams"] = new StdClass();
}
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$has = (count(array_intersect($value["disease_id"], $value_i["disease_id"]))) ? true : false;
if($has){
foreach ($value["disease_id"] as $pointer => $disease){
if (in_array($disease, $value["disease_id"])){
$newArray["disease_id"][] = $selected[$key]["disease_id"][$pointer];
unset($selected[$key]["disease_id"][$pointer]);
$cpe = "disease_cpe_".$disease;
$mbr = "disease_mbr_".$disease;
$mtr = "disease_mtr_".$disease;
if ($newArray["parpams"]->$cpe){
$newArray["parpams"]->$cpe = $newArray["parpams"]->$cpe.', '. $selected[$key]["params"]->$cpe;
} else {
$newArray["parpams"]->$cpe = $selected[$key]["params"]->$cpe;
}
if ($newArray["parpams"]->$mbr){
$newArray["parpams"]->$mbr = $newArray["parpams"]->$mbr.', '. $selected[$key]["params"]->$mbr;
} else {
$newArray["parpams"]->$mbr = $selected[$key]["params"]->$mbr;
}
if ($newArray["parpams"]->$mtr){
$newArray["parpams"]->$mtr = $newArray["parpams"]->$mtr.', '. $selected[$key]["params"]->$mtr;
} else {
$newArray["parpams"]->$mtr = $selected[$key]["params"]->$mtr;
}
unset($selected[$key]["params"]->$cpe);
unset($selected[$key]["params"]->$mbr);
unset($selected[$key]["params"]->$mtr);
}
}
}
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$has = (count(array_intersect($value["risk_id"], $value_i["risk_id"]))) ? true : false;
if($has){
foreach ($value["risk_id"] as $pointer => $risk){
if (in_array($risk, $value["risk_id"])){
$newArray["risk_id"][] = $selected[$key]["risk_id"][$pointer];
unset($selected[$key]["risk_id"][$pointer]);
$cpe = "risk_cpe_".$risk;
$mbr = "risk_mbr_".$risk;
$mtr = "risk_mtr_".$risk;
if ($newArray["parpams"]->$cpe){
$newArray["parpams"]->$cpe = $newArray["parpams"]->$cpe.', '. $selected[$key]["params"]->$cpe;
} else {
$newArray["parpams"]->$cpe = $selected[$key]["params"]->$cpe;
}
if ($newArray["parpams"]->$mbr){
$newArray["parpams"]->$mbr = $newArray["parpams"]->$mbr.', '. $selected[$key]["params"]->$mbr;
} else {
$newArray["parpams"]->$mbr = $selected[$key]["params"]->$mbr;
}
if ($newArray["parpams"]->$mtr){
$newArray["parpams"]->$mtr = $newArray["parpams"]->$mtr.', '. $selected[$key]["params"]->$mtr;
} else {
$newArray["parpams"]->$mtr = $selected[$key]["params"]->$mtr;
}
unset($selected[$key]["params"]->$cpe);
unset($selected[$key]["params"]->$mbr);
unset($selected[$key]["params"]->$mtr);
}
}
}
}
}
}
Here is another attempt:
$newArray = array();
$newArray["disease_id"] = array();
$newArray["risk_id"] = array();
if( !is_object($newArray["parpams"]) ){
$newArray["parpams"] = new StdClass();
}
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$has = (count(array_intersect($value["disease_id"], $value_i["disease_id"]))) ? true : false;
if($has){
$newArray["disease_id"] = array_merge($value["disease_id"], $value_i["disease_id"],$newArray["disease_id"]);
}
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$has = (count(array_intersect($value["risk_id"], $value_i["risk_id"]))) ? true : false;
if($has){
$newArray["risk_id"] = array_merge($value["risk_id"], $value_i["risk_id"],$newArray["risk_id"]);
}
}
}
$newArray["disease_id"] = array_unique($newArray["disease_id"]);
$newArray["risk_id"] = array_unique($newArray["risk_id"]);
}
}
out putt was:
array(3) {
["disease_id"]=>
array(14) {
[0]=>
string(1) "5"
[1]=>
string(2) "14"
[2]=>
string(2) "12"
[3]=>
string(1) "8"
[4]=>
string(2) "15"
[5]=>
string(1) "4"
[21]=>
string(2) "17"
[22]=>
string(2) "16"
[39]=>
string(1) "3"
[40]=>
string(2) "18"
[41]=>
string(1) "9"
[42]=>
string(1) "2"
[43]=>
string(2) "20"
[44]=>
string(1) "1"
}
["risk_id"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["parpams"]=>
object(stdClass)#236 (0) {
}
}
But the ['params'] are still not included.
I have read many treads on stackoverflow, but non actuality address this complexity. If I missed a thread please point me in that direction. Please do remember that these arrays can become as much as twenty arrays. Tanks!
I think this is the answer if it can be of any help to any one else.
$newArray = array();
$newArray["disease_id"] = array();
$newArray["risk_id"] = array();
$newArray["params"] = array();
foreach ($selected as $key => $value){
foreach ($selected as $key_i => $value_i){
if (is_array($value["disease_id"]) && is_array($value_i["disease_id"])){
$newArray["disease_id"] = array_merge($value["disease_id"], $value_i["disease_id"],$newArray["disease_id"]);
}
if (is_array($value["risk_id"]) && is_array($value_i["risk_id"])){
$newArray["risk_id"] = array_merge($value["risk_id"], $value_i["risk_id"],$newArray["risk_id"]);
}
}
if(is_object($value["params"])){
$newArray["params"] = array_merge_recursive((array) $value["params"], (array) $newArray["params"]);
}
}
$newArray_risk = array_unique($newArray["risk_id"]);
if(sort($newArray_risk)){
$newArray["risk_id"]= array();
foreach ($newArray_risk as $risk_id){
$newArray["risk_id"][] = $risk_id;
}
}
$newArray_disease = array_unique($newArray["disease_id"]);
if(sort($newArray_disease)){
$newArray["disease_id"] = array();
foreach ($newArray_disease as $disease_id){
$newArray["disease_id"][] = $disease_id;
}
}
foreach ($newArray["params"] as $key => $value){
if (is_array($value)){
$i=0;
foreach ($value as $val){
if (!is_array($val)){
if ($i == 0){
$newArray["params"][$key] = $val;
} else {
$newArray["params"][$key] .= ', '.$val;
}
$i++;
}
}
}
}
$newArray["params"] = (object)$newArray["params"];
}