Using odbc_fetch_array but got "arrayarrayarray "instead - php

I try to get value by using odbc_fetch_array and turn them into variable but when i try to echo it's just said "arrayarrayarray"
Array (
[0] => Array ( [PKDSEQ] => 154604 )
[1] => Array ( [PKDSEQ] => 154604 )
[2] => Array ( [PKDSEQ] => 154529 )
[3] => Array ( [PKDSEQ] => 161689 )
[4] => Array ( [PKDSEQ] => 158940 )
[5] => Array ( [PKDSEQ] => 155383 )
[6] => Array ( [PKDSEQ] => 156247 )
[7] => Array ( [PKDSEQ] => 158123 )
)
and is there a way to get array separate into number?
Code
$PKDSEQ2 = array();
$table4 = "SELECT [PKDSEQ] FROM [PWSWMS].[dbo].[tbTR_PACKD] WHERE [PKDSEQ] = '$PKDSEQRS5'";
$RS4 = odbc_exec($connection2, $table4);
while ($PKDSEQ2 = odbc_fetch_array($RS4)) {
$PKDSEQ[] = $PKDSEQ2;
}
}
print_r(array_values($PKDSEQ));
if(isset($_POST['QTYINPUT1'])) {
$QTYINPUT1 = $_POST['QTYINPUT1'];
$update = "UPDATE [PWSWMS].[dbo].[tbTR_PACKD] SET QTYPCK='$QTYINPUT1' WHERE [PKDSEQ]='$PKDSEQ[1]'";
$result = odbc_exec($connection2, $update);
echo "<br>$QTYINPUT1";
echo "<br>$PKDSEQ[1]";
}

You've got an associative array within each entry in $PKDSEQ. So you need to refer to the property of that array which you want to output. Since the array happens to only have one property, it's pretty simple to choose:
echo "<br>".$PKDSEQ[1]["PKDSEQ"];

Related

Dynamically put condition with the array as per the array value

I have the below array. I want to put condition dynamically as per the sections are coming.
stdClass Object
(
[content_form] => Array
(
[0] => genre
[1] => cast
[2] => category
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
[category] => Array
(
[0] => 4119
[1] => 4118
[2] => 4081
)
)
[conditions] => Array
(
[0] => OR
[1] => AND
)
)
In the above array for content_form there are 3 array values and in 0th index genre, 1st index cast and 2nd index category present.
Similarly for content_type there are 3 array values present and in the last array conditions present.
My requirement is that as per the section coming I want to put condition with them.
Example- ((genre OR cast) AND category)
similarly if my output is like below then the condition will be
Example - (genre OR cast)
[content_form] => Array
(
[0] => genre
[1] => cast
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
[0] => OR
)
)
And if my output is like the below then there will no condition with any other key and it will return the simple result.
stdClass Object
(
[content_form] => Array
(
[0] => cast
)
[content_type] => stdClass Object
(
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
)
)
How I can do this dynamically as per the sections are coming as per sequence with the array of content_type and conditions?
We have done this. Please check:
$jsonData = '{"content_form":["genre","cast","category", "prakash"],"content_type":{"genre":["History","ACTION","ROMANTIC"],"cast":["13128","13127"],"category":["4119","4118","4081"]},"conditions":["OR","AND","OR"]}';
$contentIds = array(
'genre'=>array(1,2,3),
'cast'=>array(1,2,4),
'category'=>array(3,2,7),
'prakash'=>array(1,2,3,8,9)
);
$arrayDecode = json_decode($jsonData,true);
$result = array();
foreach($arrayDecode['conditions'] as $key=>$val){
if($result){
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($result, $secondArr);
} else {
$firstArr = $contentIds[$arrayDecode['content_form'][$key]];
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($firstArr, $secondArr);
}
if($val=='OR'){
$result = array_unique($result);
} else {
$result = array_diff_assoc($result, array_unique($result));
}
}
$result = array_values($result);
print_r($result);
Let me know this is your requirement or not?

Populate the array in php using mysql dynamically

I have the database and i want to create a matrix or probably 2d array in PHP where the first column comes from first query and then i loop again another query. I am getting it but when it comes to the matrix i want first column populate from the first column and the rest of the column in that row should populate depending on the data for that particular query. you might better understand after looking at the code
<!DOCTYPE html>
<html>
<?php
require ('connect.php');
$pro_name = $_POST['project'];
$sheet_type = $_POST['sheet_set'];
echo $pro_name ."-";
echo $sheet_type . "<br/>";
?>
<?php
$doc_num_data = array();
$qry_doc_num = "SELECT DISTINCT document_number FROM `$pro_name`.Instruments";
$result_doc_num = mysqli_query($connection, $qry_doc_num) or die(mysqli_error($connection));
while($doc_num = mysqli_fetch_row($result_doc_num)){
if($doc_num[0] != ''){
$qry_ins = "SELECT CONCAT(tag_letters,'-',tag_numbers) FROM `$pro_name`.Instruments WHERE document_number = '$doc_num[0]'";
$result_qry_ins = mysqli_query($connection, $qry_ins) or die(mysqli_error($connection));
// array_push($doc_num_data, $doc_num);
// $doc_num_data = array();
while($ins_doc = mysqli_fetch_row($result_qry_ins)){
$instruments_tag = $ins_doc[0];
array_push($doc_num_data, $doc_num);
}
}
}
echo "<pre>";
print_r($doc_num_data);
echo "</pre>";
?>
</html>
I want something like this
[doc1] [ins1] [ins2] []
[doc2] [ins1] [] []
[doc3] [ins1] [ins2] [ins3]
[doc4] [ins1] [ins2] []
thats the result what i get basically each element is saving in different array
Array (
[0] => Array
(
[0] => test1
)
[1] => Array
(
[0] => PIT-100
)
[2] => Array
(
[0] => test1
)
[3] => Array
(
[0] => PIT-330
)
[4] => Array
(
[0] => PIT-330
)
[5] => Array
(
[0] => PIT-330
)
[6] => Array
(
[0] => test2
)
[7] => Array
(
[0] => PIT-300
)
[8] => Array
(
[0] => PIT-300
)
[9] => Array
(
[0] => PIT-300
)
[10] => Array
(
[0] => PIT-300
)
[11] => Array
(
[0] => PIT-300
)
The problem you're having is with array_push(). All that is going to do (as you can see) is stack the values into a LIST, not a hash / multi-dimensional array.
What you want is a list of things that are referenced by that doc ID, right?
Try this:
if($instruments_tag != null) array_push($doc_num_data[$doc_num], $instruments_tag);
I think that is what you're looking for, or close to it at any rate, and it won't add that empty value at the end. If you want that, remove the if.
Array
(
[0] => Array
(
[0] => test1
[1] => PIT-100
[2] => PIT-330
)
[1] => Array
(
[0] => test2
[1] => PIT-300
)
[2] => Array
(
[0] => test3
[1] => TIT-100
)
[3] => Array
(
[0] => test5
[1] => TIT-200
[2] => TIT-900
[3] => PIT-000
)
[4] => Array
(
[0] => test4
[1] => TIT-1000
)
[5] => Array
(
[0] => test6
[1] => TIT-999
)
)
using this code
<?php
$doc_num_data = array();
$qry_doc_num = "SELECT DISTINCT document_number FROM `$pro_name`.Instruments";
$result_doc_num = mysqli_query($connection, $qry_doc_num) or die(mysqli_error($connection));
$i=0;
while($doc_num = mysqli_fetch_row($result_doc_num)){
$j=1;
if($doc_num[0] != ''){
$qry_ins = "SELECT CONCAT(tag_letters,'-',tag_numbers) FROM `$pro_name`.Instruments WHERE document_number = '$doc_num[0]'";
$result_qry_ins = mysqli_query($connection, $qry_ins) or die(mysqli_error($connection));
//echo $doc_num[0];
$doc_num_data[$i][0] = $doc_num[0];
//echo $doc_num_data[$i];
// $doc_num_data = array();
while($ins_doc = mysqli_fetch_row($result_qry_ins)){
$instruments_tag = $ins_doc[0];
$doc_num_data[$i][$j]= $instruments_tag;
$j=$j+1;
}
}
$i= $i+1;
}

Sort Data in php array according to some key

I want to sort my array according to idai value. I've tried to use SORT,KSORT,USORT but nothing useful.
Here is my data
Array
(
[0] => Array
(
[0] => Array
(
[idai] => 4
[id] => 6187
[name] => xyz
)
[1] => Array
(
[idai] => 5
[id] => 5256
[name] => abc
)
[2] => Array
(
[idai] => 10
[id] => 21921
[name] => qwe
)
[3] => Array
(
[idai] => 6
[id] => 29679
[name] => IOU
)
[4] => Array
(
[idai] => 11
[id] => 21062
[name] => STU
)
)
)
And I'm not sure why I'm getting this nested array..
Here is my how I declared my array:
$return_arr = array();
$return_arr['feed'] = array();
My code to store data in array from my db
$query = "SELECT * FROM user_post WHERE userid = '$friend_id'";
$result = mysql_query($query);
while( $row = mysql_fetch_array($result) ) {
$row_array['idai'] = $row['id'];
$row_array['id'] = $row['post_id'];
$row_array['name'] = $pic['name'];
array_push($return_arr['feed'],$row_array);
}
Try this code:
$return_arr['feed'][$row['id']] = $row_array;
or if you want not nested array:
$return_arr[$row['id']] = $row_array;
it will automaticly sort as array key is id

How to change the array in following scenario?

I've an SQL query as follows:
SELECT test_user_user_id FROM OCN.tests_users WHERE test_user_test_id = 99
$this->mDb->Query( $sql );
$students_data = $this->mDb->FetchArray();
I get the array as follows:
Array
(
[0] => Array
(
[test_user_user_id] => b9e6493f9f8599bc5a0a5935275228c2
)
[1] => Array
(
[test_user_user_id] => 395599c5891c1418357e2efa89bc3e27
)
[2] => Array
(
[test_user_user_id] => 3255605bb9fd3ecc0295a1bfb3cba147
)
[3] => Array
(
[test_user_user_id] => ebe6711fc156b2cc1a33f64f3d86150f
)
[4] => Array
(
[test_user_user_id] => 627b9c3f21d93f1e13af076cff20b143
)
[5] => Array
(
[test_user_user_id] => 030e96561c01afde1c46384f57cf8749
)
[6] => Array
(
[test_user_user_id] => 9def02e6337b888d6dbe5617a172c18d
)
)
Actually I want the array in following format:
Array
(
[0] => b9e6493f9f8599bc5a0a5935275228c2
[1] => 395599c5891c1418357e2efa89bc3e27
[2] => 3255605bb9fd3ecc0295a1bfb3cba147
[3] => ebe6711fc156b2cc1a33f64f3d86150f
[4] => 627b9c3f21d93f1e13af076cff20b143
[5] => 030e96561c01afde1c46384f57cf8749
[6] => 9def02e6337b888d6dbe5617a172c18d
)
How to get the array in above format?
$resArray = Array();
foreach($myArr as $_arr)
{
$resArray[] = $_arr['test_user_user_id'];
}
You could use array_map:
function user_id($e)
{
return $e['test_user_user_id'];
}
$students_data = array_map('user_id', $students_data);
$new_array = array();
$counter = 0; // optional... because by default starting index is 0... however if you want to change the index then use a counter
foreach($students_data as $row) {
$new_array[$counter] = $row['test_user_user_id'];
$counter++;
}
print_r($new_array);

How do you add to a new index on a Multi-dimensional array within a foreach loop?

Hell once again, I am wondering how do you go about adding data to a new array index when inside a foreach loop?
the code I have atm is,
// Connect to the database to gather all data pertaiing to the link in question
$assoResult = mysql_query("SELECT * FROM associate_users");
while ($assoRow = mysql_fetch_field($assoResult)) {
$resultArray[] = $assoRow->name;
}
// Connect to the database to gather all data pertaiing to the link in question
$assoResult2 = mysql_query("SELECT * FROM associate_users WHERE id='$getID'");
while ($assoRow2 = mysql_fetch_object($assoResult2)) {
foreach ($resultArray as $row) {
$array = array(array( 1 => $assoRow2->$row, 2 => $row, ),);
echo "<br />"; print_r($array);
}
}
Below is the outputted data that comes from the "echo "br />"; print_r($array);" line.
=================================================================
Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [0] => Array ( [1] => Bob[2] => contactName ) )
Array ( [0] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [0] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [0] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [0] => Array ( [1] => XXXXXX [2] => postcode ) )
As you can see the array is being created a new over and over, what I need is for the above data to increment the 1st dimension index key on every loop, so it looks like...
=================================================================
Array ( [0] => Array ( [1] => 1 [2] => id ) )
Array ( [1] => Array ( [1] => Bob[2] => contactName ) )
Array ( [2] => Array ( [1] => Bob's Tyres [2] => company ) )
Array ( [3] => Array ( [1] => XXXXXXXXXXXXXX [2] => address1 ) )
Array ( [4] => Array ( [1] => XXXXXXXXXXXXXX [2] => address2 ) )
Array ( [5] => Array ( [1] => XXXXXXXXX [2] => address3 ) )
Array ( [6] => Array ( [1] => XXXXXX [2] => postcode ) )
Thank you in advance I am out of all options in getting this to work and desperate.
Dan.
Change the values assigning part of your code to
$count=0;
foreach ($resultArray as $row) {
$array[$count][1] = $assoRow2->$row
$array[$count][2]=$row;
$count++;
echo "<br />"; print_r($array);
}
This code gets you the output you asked for without inefficiently using two queries:
// Connect to the database to gather all data pertaining to the link in question
$result = mysql_query("SELECT * FROM associate_users WHERE id=" . (int)$getID);
$resultArray = array();
$resultCount = 0;
$row = mysql_fetch_assoc($result);
$count = 0;
foreach ($row as $key => $value) {
$temp = array();
$temp[$count] = array(1 => $value, 2 => $key);
$count++;
echo "<br />"; print_r($temp);
}
Why you want it like this, I have no idea.
//extra code.declaring array
$array = array();
while ($assoRow2 = mysql_fetch_object($assoResult2)) {
foreach ($resultArray as $row) {
// 1st parameter is the array name, here array name is array .give gd name according to use
array_push($array,array( 1 => $assoRow2->$row, 2 => $row, ));
echo "<br />"; print_r($array);
}
}

Categories