Getting data out of an array - php

What would be my best option to get the data out of this array?
array(4) {
[0]=> array(10) {
["id"]=> string(3) "158"
["name"]=> string(8) "Tractors"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "37"
["blurb"]=> string(17) "Agrilife Tractors"
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "1"
["sorder"]=> string(1) "0"
["state"]=> string(1) "1"
}
[1]=> array(10) {
["id"]=> string(3) "159"
["name"]=> string(8) "Ride Ons"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "74"
["blurb"]=> string(0) ""
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "2"
["sorder"]=> string(1) "1"
["state"]=> string(1) "1"
}
[2]=> array(10) {
["id"]=> string(3) "160"
["name"]=> string(9) "Machinery"
["parent_id"]=> string(1) "0"
["image_id"]=> string(2) "14"
["blurb"]=> string(0) ""
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "3"
["sorder"]=> string(1) "2"
["state"]=> string(1) "1"
}
[3]=> array(10) {
["id"]=> string(3) "161"
["name"]=> string(17) "Outdoor Equipment"
["parent_id"]=> string(1) "0"
["image_id"]=> string(3) "114"
["blurb"]=> NULL
["brand_name"]=> string(4) "SAME"
["brand_id"]=> string(1) "2"
["cat_id"]=> string(1) "5"
["sorder"]=> string(1) "3"
["state"]=> string(1) "1"
}
}
Tractors
My HTML looks like this I am trying to foreach to get all of the relevant data out so I can echo when or where needed.
HTML:
foreach($assoc_categories as $assoc_cat)
{
// Page load - does assoc exist?
$checked_state = "";
$does_assoc_exist = $this->Ps_products_model->brand_specific_cat_assoc_exist($brand_id, $assoc_cat['id']);
if($does_assoc_exist == "1")
{
$checked_state = " checked='checked'";
}
?>
<div>
<input type="checkbox" name="product_category" class="product_category_selector" id="product_category_<?php echo $assoc_cat['id']; ?>" data-id="<?php echo $assoc_cat['id']; ?>" <?php echo $checked_state; ?> /> <?php echo $assoc_cat['name']; ?>
</div>
<input class="order" type="input" />
<?php
}
?>

To dump all the values, you'd need nested foreach like this:
foreach ($original_array as $sub_array) {
foreach ($sub_array as $key=>$value) {
echo $key.' '.$value.'<br>';
}
}
To get just one value, you need to access it with its address. It may not be set, so check first:
foreach ($original_array as $sub_array) {
// Say you want all the `name`s
if (isset($sub_array['name'])) {
echo $sub_array['name'].'<br>';
}
}

Related

Get specific value from PHP array using foreach key value

I have the following array:
array(15) {
[0]=> object(stdClass)#317 (2) { ["id"]=> string(1) "2" ["value"]=> string(1) "1" }
[1]=> object(stdClass)#316 (2) { ["id"]=> string(1) "3" ["value"]=> string(531) "awfaww" }
[2]=> object(stdClass)#315 (2) { ["id"]=> string(1) "4" ["value"]=> string(1) "1" }
[3]=> object(stdClass)#318 (2) { ["id"]=> string(1) "5" ["value"]=> string(1) "1" }
[4]=> object(stdClass)#319 (2) { ["id"]=> string(1) "6" ["value"]=> string(1) "1" }
[5]=> object(stdClass)#320 (2) { ["id"]=> string(1) "7" ["value"]=> string(1) "1" }
[6]=> object(stdClass)#321 (2) { ["id"]=> string(1) "8" ["value"]=> string(1) "1" }
[7]=> object(stdClass)#322 (2) { ["id"]=> string(2) "30" ["value"]=> string(8) "12:30:02" }
[8]=> object(stdClass)#323 (2) { ["id"]=> string(2) "31" ["value"]=> string(8) "18:12:00" }
[9]=> object(stdClass)#324 (2) { ["id"]=> string(2) "11" ["value"]=> string(10) "2014-06-17" }
[10]=> object(stdClass)#325 (2) { ["id"]=> string(2) "12" ["value"]=> string(10) "2014-06-26" }
[11]=> object(stdClass)#326 (2) { ["id"]=> string(2) "14" ["value"]=> string(1) "2" }
[12]=> object(stdClass)#327 (2) { ["id"]=> string(2) "15" ["value"]=> string(1) "2" }
[13]=> object(stdClass)#328 (2) { ["id"]=> string(2) "16" ["value"]=> string(1) "4" }
[14]=> object(stdClass)#329 (2) { ["id"]=> string(2) "17" ["value"]=> string(1) "5" }
}
I would like to get a specific value from this array using the ID. For example, if the ID: 11 is found in the array I want to retrieve its value. How can I do this?
Try something like this:
<?php
function findById($array, $id) {
foreach ($array as $value) {
if ($value->id == $id) {
return $value->value;
}
}
return null;
}
$result = findById($yourArray, 11);
?>
if the array is static for each run - i'd suggest changing the array into "key"=>"value" array, using this:
$new_arr = array();
foreach($original_array as $object) {
$new_arr[$object->id] = $object->value;
}
and then you can just use $new_arr[id], instead of searching the whole original array each time.
You can use array_filter:
array_filter($arr, function($i) { return $i->id == '11'; });
See Documentation

Select all rows from table in Yii

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;
}

Merging multidemational Array so that the values get preserved but duplicates on both sides merged

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"];
}

Secondary Drop Down with Preloaded options

I have Sub-Categories that are related to a Main Category. My sub cat table has the main category id as a foreign key. I'd like to preload all subcategories using javascript and avoid an extra php file or db query if possible.
Both dropdowns are listing all the records but...
I'd like my second dropdown list of sub-categories to change dynamically onChange (select) of the first dropdown based on the first dropdown's value which is the main cat id.
Example,
$data = $this->Category->MainCategory->findall();
$this->set('data',$data);
$data2 = $this->Category->SubCategory->findall();
$this->set('data2',$data2);
My arrays look like this:
var_dump ($data) =
array(5) {
[0]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "1" ["name"]=> string(10) "Accounting" ["doctype"]=> string(1) "2" } }
[1]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "2" ["name"]=> string(15) "Human Resources" ["doctype"]=> string(1) "2" } }
[2]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "4" ["name"]=> string(5) "Clubs" ["doctype"]=> string(1) "2" } }
[3]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "8" ["name"]=> string(16) "Service" ["doctype"]=> string(1) "2" } }
[4]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(2) "10" ["name"]=> string(9) "Safety" ["doctype"]=> string(1) "2" } } }
var_dump($data2) =
array(20) {
[0]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "1" ["name"]=> string(17) "Application Forms" ["main_category_id"]=> string(1) "2" } }
[1]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "2" ["name"]=> string(19) "Benefit Claim Forms" ["main_category_id"]=> string(1) "2" } }
[2]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "3" ["name"]=> string(22) "Evaluations" ["main_category_id"]=> string(1) "2" } }
[3]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "4" ["name"]=> string(11) "Leave Forms" ["main_category_id"]=> string(1) "2" } }
[4]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "5" ["name"]=> string(13) "Payroll" ["main_category_id"]=> string(1) "2" } }
[5]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "6" ["name"]=> string(17) "Recruitment" ["main_category_id"]=> string(1) "2" } }
[6]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "7" ["name"]=> string(24) "Training" ["main_category_id"]=> string(1) "2" } }
[7]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "8" ["name"]=> string(10) "Accounting" ["main_category_id"]=> string(1) "1" } }
[8]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "9" ["name"]=> string(13) "Staff" ["main_category_id"]=> string(1) "2" } }
[9]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "10" ["name"]=> string(14) "Codes" ["main_category_id"]=> string(2) "3" } }
[10]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "11" ["name"]=> string(28) "Reports" ["main_category_id"]=> string(2) "3" }
[11]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "12" ["name"]=> string(14) "Plan" ["main_category_id"]=> string(2) "4" } }
[12]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "13" ["name"]=> string(21) "Charts" ["main_category_id"]=> string(2) "4" } }
[13]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "14" ["name"]=> string(11) "Travel" ["main_category_id"]=> string(1) "4" } }
[14]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "15" ["name"]=> string(15) "Financials" ["main_category_id"]=> string(1) "4" } }
[15]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "16" ["name"]=> string(19) "Event Planning" ["main_category_id"]=> string(1) "4" } }
[16]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "17" ["name"]=> string(14) "Resources" ["main_category_id"]=> string(1) "4" } }
[17]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "18" ["name"]=> string(11) "Basics" ["main_category_id"]=> string(1) "4" } }
[18]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "19" ["name"]=> string(9) "News" ["main_category_id"]=> string(1) "4" } }
[19]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "20" ["name"]=> string(12) “Funding" ["main_category_id"]=> string(1) "4" } } }
--html form ….
<td>Category: </td>
<td><select id="main_cat">
<option value=""> </option>
<?php foreach($data as $row){
echo "<option value=".$row['MainCategory']['id'].">" .$row['MainCategory']['name']. "</option>";
} ?>
</select></td>
<td>Sub Category: </td>
<td><select id="sub_cat">
<option value=""> </option>
<?php foreach($data2 as $row){
echo "<option value=".$row[‘SubCategory']['id'].">" .$row['SubCategory']['name']. "</option>";
} ?>
</select></td>
…..
Create a two dimensional javascript array loaded with the sub-categories by category id, then reload the second drop down when the first changes. Highly simplified jsfiddle example.
For the php, you'll only need to worry about generating the subCats array. Something along the lines of:
<script type="text/javascript>
var subCats = [
<?php foreach($data2 as $row){
echo "[";
foreach($row as $subcat) {
echo "\"" . $subcat . "\",";
}
echo "]";
} ?>
</script>
just.another.programmer got me on the right track and with some other examples from Jonathan Kuhn, this is the result http://jsfiddle.net/wprLD/9/
<script type="text/javascript">
var subCats =
<?php
$js = array();
foreach($data2 as $sub){
//get the parent id (main_category_id)
$parent = $sub['SubCategory']['main_category_id'];
//if the parent doesn't exist, add it
if(!isset($js[$parent])){
//add array with name and id
$js[$parent] = array(array('id'=>$sub['SubCategory']['id'],'name'=>$sub['SubCategory']['name']));
//parent does exist
} else {
//append this entry name and id
$js[$parent][] = array('id'=>$sub['SubCategory']['id'],'name'=>$sub['SubCategory']['name']);
}
}
$js = array_values($js);
echo json_encode($js);
?>
;
function makeSubCatHtml(catId) {
var subCatHtml = "",
i;
//check if the subcats object has this cat Id.
if(subCats.hasOwnProperty(catId)){
for (i in subCats[catId]) {
subCatHtml += "<option value='"+subCats[catId][i].id+"'>" + subCats[catId][i].name + "</option>";
}
}
return subCatHtml;
}
$(document).ready(function () {
$("#MainCategory").bind("change", function () {
$("#SubCategory").html(
makeSubCatHtml(this.selectedIndex));
});
});
</script>

PHP Group By Day

Given this array:
array(1) {
[0]=>
array(2) {
["Project"]=>
array(5) {
["id"]=>
string(1) "2"
["user_id"]=>
string(2) "21"
["customer_id"]=>
string(1) "4"
["name"]=>
string(15) "WordPress Theme"
["created"]=>
string(19) "2011-09-26 21:30:38"
}
["Track"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "7"
["user_id"]=>
string(2) "21"
["project"]=>
string(1) "2"
["customer"]=>
string(1) "4"
["title"]=>
string(7) "Backend"
["notes"]=>
string(0) ""
["created"]=>
string(19) "2011-09-28 22:21:22"
["Lapse"]=>
array(2) {
[0]=>
array(5) {
["id"]=>
string(1) "4"
["track_id"]=>
string(1) "7"
["start"]=>
string(19) "2011-09-28 22:22:21"
["stop"]=>
string(19) "2011-09-28 22:22:30"
["created"]=>
string(19) "2011-09-28 22:22:21"
}
[1]=>
array(5) {
["id"]=>
string(1) "3"
["track_id"]=>
string(1) "7"
["start"]=>
string(19) "2011-09-28 22:22:07"
["stop"]=>
string(19) "2011-09-28 22:22:12"
["created"]=>
string(19) "2011-09-28 22:22:07"
}
}
}
}
}
}
How would i group by Day in the Lapse Array with PHP? This may have been easier to do directly with MySQL but i'm using CakePHP's recursive function and i cant figure out how to use Group By with that!
$list = array();
function extractByDates($arr) {
foreach ($arr as $key => $v)
if (is_array($v))
function extractByDates($v);
else if ($key == 'created')
$list[$v] = $v;
}
extractByDates($yourGivenArray);
I not tested!

Categories