Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 07-22-2009, 03:20 PM I Need Array Help
MoForce's Avatar
Super Talker

Posts: 145
Name: Jack Shalt
Trades: 0
This should be simple, but I can't think of a solution right now =P

I have an array like so
array(
['something'] => ['other'] => blah
)

imagine thousands of entries like that with the "blah"s varying and "['something'] => ['other'] =>" staying the same

Here's the question: How can I figure out positioning with the array? I figured the best way to do it was to merge with another array that contains something like..

array(
[0]
[1]
[2]
...
)

so it can become like

array(
[0] => ['something'] => ['other'] => blah
[1] => ['something'] => ['other'] => meh
)
MoForce is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-22-2009, 03:41 PM Re: I Need Array Help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
A more concrete explanation of what you are trying to do might make this easier to answer, but the short answer is, yes you can do it like this:

PHP Code:
$arr = array(
          array( 
'something' => array( 'other' => 'blah' ) )
          ); 
Honestly, however, if you find yourself using that approach you may want to stop and rethink your method. Maybe using an object would be more appropriate.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-22-2009, 04:19 PM Re: I Need Array Help
MoForce's Avatar
Super Talker

Posts: 145
Name: Jack Shalt
Trades: 0
Quote:
Originally Posted by NullPointer View Post
A more concrete explanation of what you are trying to do might make this easier to answer, but the short answer is, yes you can do it like this:

PHP Code:
$arr = array(
          array( 
'something' => array( 'other' => 'blah' ) )
          ); 
Honestly, however, if you find yourself using that approach you may want to stop and rethink your method. Maybe using an object would be more appropriate.
Yeah you're right, I'll try a different method.

Thanks
MoForce is offline
Reply With Quote
View Public Profile
 
Old 07-22-2009, 06:22 PM Re: I Need Array Help
MoForce's Avatar
Super Talker

Posts: 145
Name: Jack Shalt
Trades: 0
looks like I'll need help with a new method.

How can I select out of an array values surrounding another value?

Example: 1,2,3,4,5,6,7,8,9,10 (values in an array)

Based on the user input you get the adjacent values. So lets say I choose the #8 I should then get an output of 6,7,8,9,10.

Can this be done?
MoForce is offline
Reply With Quote
View Public Profile
 
Old 07-22-2009, 07:45 PM Re: I Need Array Help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Here is one way:

PHP Code:
$range 2;
$input//assume this variable contains the user's input
$arr//assume this is the array

for($i = ($input $range); $i <= ($input $range); $i++)
{
     echo 
$arr[$i];

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-26-2009, 02:07 AM Re: I Need Array Help
Experienced Talker

Posts: 41
Trades: 0
if you mean a array inside of another array you need to use qoutes and brackets on it like

$a = 0,1,2,3,4,5,6,7,8,9,10,11;
$b =-1,0,1,2,3,4,5,6,7,8,9,10;

$c = $a.$b;


echo "{$c[2][6]}";
would return as a 5

Last edited by flatrat; 07-26-2009 at 02:11 AM.. Reason: forgot the ; like always
flatrat is offline
Reply With Quote
View Public Profile
 
Old 07-26-2009, 10:53 PM Re: I Need Array Help
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by flatrat View Post
if you mean a array inside of another array you need to use qoutes and brackets on it like
Quotes and brackets are used when variables need to be parsed in a string. If all you need to do is return the value then there is no reason to use them.

Quote:
$a = 0,1,2,3,4,5,6,7,8,9,10,11;
$b =-1,0,1,2,3,4,5,6,7,8,9,10;
Arrays are declared using the array( ) "function" (its actually a language construct).

PHP Code:
$a = array(1,2,3); 
Quote:
$c = $a.$b;
I'm not sure what you expect this line to do. As far as I'm aware "." is not an array operator.

Quote:
echo "{$c[2][6]}";
As I mentioned earlier, the quotes and brackets are not necessary. The following code will have the same effect:

PHP Code:
echo $c[2][6]; 
Strings encased in double quotes indicate that there is a variable that needs to be parsed. Brackets are used to separate variables from from the surrounding text.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to I Need Array Help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25869 seconds with 12 queries